-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0609556
commit 4b3b953
Showing
6 changed files
with
87 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>WebSocket Example</title> | ||
<script src="https://unpkg.com/@mallpopstar/partyline@latest/dist/partyline.min.js"></script> | ||
<script src="https://unpkg.com/@mallpopstar/rc@latest/dist/rc.min.js"></script> | ||
</head> | ||
<body> | ||
<form> | ||
<input type="text" name="name" /> | ||
<input type="submit" value="Submit" /> | ||
</form> | ||
|
||
<script> | ||
const { createReceiver, createSender } = partyline | ||
const { createDocumentReceiver, createDocumentSender } = remotecontrol | ||
const subscribers = new Map() | ||
|
||
const receiver = createReceiver() | ||
receiver.connect(window) | ||
createDocumentReceiver(receiver).start() | ||
|
||
const sender = createSender() | ||
sender.connect(window) | ||
const documentSender = createDocumentSender(sender) | ||
|
||
// Connect to web socket | ||
const socket = new WebSocket('ws://localhost:8000') | ||
|
||
socket.addEventListener('open', () => { | ||
console.log('Connected to server') | ||
}) | ||
|
||
socket.addEventListener('message', evt => { | ||
const { type, selector, event } = JSON.parse(evt.data) | ||
if (type === 'subscribe') { | ||
const unsub = documentSender.subscribe( | ||
selector, | ||
event, | ||
req => { | ||
socket.send(JSON.stringify(req)) | ||
}, | ||
{ preventDefault: true } | ||
) | ||
subscribers.set(`${selector}:${event}`, unsub) | ||
} else if (type === 'unsubscribe') { | ||
console.log('Unsubscribing', selector, event) | ||
const unsub = subscribers.get(`${selector}:${event}`) | ||
if(unsub) unsub() | ||
} else { | ||
console.log('Unknown message type', type) | ||
} | ||
}) | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// deno run --allow-net server.ts | ||
Deno.serve(req => { | ||
if (req.headers.get('upgrade') !== 'websocket') { | ||
return new Response(null, { status: 501 }) | ||
} | ||
|
||
const { socket, response } = Deno.upgradeWebSocket(req) | ||
socket.addEventListener('open', () => { | ||
console.log('a client connected!') | ||
socket.send(JSON.stringify({ type: 'subscribe', selector: 'form', event: 'submit' })) | ||
}) | ||
|
||
socket.addEventListener('message', event => { | ||
const payload = JSON.parse(event.data) | ||
console.log(payload) | ||
if(payload.body.selector === 'form' && payload.body.type === 'submit') { | ||
console.log('unsubscribing from form') | ||
socket.send(JSON.stringify({ type: 'unsubscribe', selector: 'form', event: 'submit' })) | ||
} | ||
}) | ||
return response | ||
}) |