-
-
Notifications
You must be signed in to change notification settings - Fork 121
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
Showing
14 changed files
with
2,717 additions
and
220 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,3 @@ | ||
dist | ||
node_modules | ||
bower_components | ||
build | ||
build_test | ||
.directory | ||
.codio | ||
.settings | ||
.jshintignore | ||
.jshintrc | ||
.validate.json | ||
/y.js | ||
/y.js.map | ||
/y-* | ||
.vscode |
This file was deleted.
Oops, something went wrong.
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,141 @@ | ||
#!/usr/bin/env node | ||
|
||
const ws = require('ws') | ||
const http = require('http') | ||
const map = require('lib0/dist/map.js') | ||
|
||
const wsReadyStateConnecting = 0 | ||
const wsReadyStateOpen = 1 | ||
const wsReadyStateClosing = 2 // eslint-disable-line | ||
const wsReadyStateClosed = 3 // eslint-disable-line | ||
|
||
const pingTimeout = 30000 | ||
|
||
const port = process.env.PORT || 4444 | ||
// @ts-ignore | ||
const wss = new ws.Server({ noServer: true }) | ||
|
||
const server = http.createServer((request, response) => { | ||
response.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
response.end('okay') | ||
}) | ||
|
||
/** | ||
* Map froms topic-name to set of subscribed clients. | ||
* @type {Map<string, Set<any>>} | ||
*/ | ||
const topics = new Map() | ||
|
||
/** | ||
* @param {any} conn | ||
* @param {object} message | ||
*/ | ||
const send = (conn, message) => { | ||
if (conn.readyState !== wsReadyStateConnecting && conn.readyState !== wsReadyStateOpen) { | ||
conn.close() | ||
} | ||
try { | ||
conn.send(JSON.stringify(message)) | ||
} catch (e) { | ||
conn.close() | ||
} | ||
} | ||
|
||
/** | ||
* Setup a new client | ||
* @param {any} conn | ||
*/ | ||
const onconnection = conn => { | ||
/** | ||
* @type {Set<string>} | ||
*/ | ||
const subscribedTopics = new Set() | ||
let closed = false | ||
// Check if connection is still alive | ||
let pongReceived = true | ||
const pingInterval = setInterval(() => { | ||
if (!pongReceived) { | ||
conn.close() | ||
clearInterval(pingInterval) | ||
} else { | ||
pongReceived = false | ||
try { | ||
conn.ping() | ||
} catch (e) { | ||
conn.close() | ||
} | ||
} | ||
}, pingTimeout) | ||
conn.on('pong', () => { | ||
pongReceived = true | ||
}) | ||
conn.on('close', () => { | ||
subscribedTopics.forEach(topicName => { | ||
const subs = topics.get(topicName) || new Set() | ||
subs.delete(conn) | ||
if (subs.size === 0) { | ||
topics.delete(topicName) | ||
} | ||
}) | ||
subscribedTopics.clear() | ||
closed = true | ||
}) | ||
conn.on('message', /** @param {object} message */ message => { | ||
if (typeof message === 'string') { | ||
message = JSON.parse(message) | ||
} | ||
if (message && message.type && !closed) { | ||
switch (message.type) { | ||
case 'subscribe': | ||
/** @type {Array<string>} */ (message.topics || []).forEach(topicName => { | ||
if (typeof topicName === 'string') { | ||
// add conn to topic | ||
const topic = map.setIfUndefined(topics, topicName, () => new Set()) | ||
topic.add(conn) | ||
// add topic to conn | ||
subscribedTopics.add(topicName) | ||
} | ||
}) | ||
break | ||
case 'unsubscribe': | ||
/** @type {Array<string>} */ (message.topics || []).forEach(topicName => { | ||
const subs = topics.get(topicName) | ||
if (subs) { | ||
subs.delete(conn) | ||
} | ||
}) | ||
break | ||
case 'publish': | ||
if (message.topics) { | ||
/** | ||
* @type {Set<any>} | ||
*/ | ||
const receivers = new Set() | ||
message.topics.forEach(/** @param {string} topicName */ topicName => { | ||
(topics.get(topicName) || []).forEach(sub => receivers.add(sub)) | ||
}) | ||
receivers.forEach(receiver => send(receiver, message)) | ||
} | ||
break | ||
case 'ping': | ||
send(conn, { type: 'pong' }) | ||
} | ||
} | ||
}) | ||
} | ||
wss.on('connection', onconnection) | ||
|
||
server.on('upgrade', (request, socket, head) => { | ||
// You may check auth of request here.. | ||
/** | ||
* @param {any} ws | ||
*/ | ||
const handleAuth = ws => { | ||
wss.emit('connection', ws, request) | ||
} | ||
wss.handleUpgrade(request, socket, head, handleAuth) | ||
}) | ||
|
||
server.listen(port) | ||
|
||
console.log('Signalling server running on localhost:', port) |
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,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"/> | ||
<title>y-webrtc demo</title> | ||
</head> | ||
<body> | ||
<button type="button" id="y-connect-btn">Disconnect</button> | ||
<script type="text/javascript" src="../dist/demo.js"></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,19 @@ | ||
/* eslint-env browser */ | ||
|
||
import * as Y from 'yjs' | ||
import { WebrtcProvider } from '../src/y-webrtc.js' | ||
|
||
const ydoc = new Y.Doc() | ||
const provider = new WebrtcProvider('prosemirror', ydoc) | ||
const yarray = ydoc.get('prosemirror', Y.XmlFragment) | ||
|
||
provider.on('synced', synced => { | ||
console.log('synced!', synced) | ||
}) | ||
|
||
yarray.observeDeep(() => { | ||
console.log('yarray updated: ', yarray.toJSON()) | ||
}) | ||
|
||
// @ts-ignore | ||
window.example = { provider, ydoc, yarray } |
Submodule dist
deleted from
0f203f
This file was deleted.
Oops, something went wrong.
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,4 @@ | ||
{ | ||
"name": "y-webrtc", | ||
"version": 1 | ||
} |
Oops, something went wrong.