Skip to content

Commit

Permalink
rewrite for v13
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Dec 5, 2019
1 parent 1c6559b commit 1fac438
Show file tree
Hide file tree
Showing 14 changed files with 2,717 additions and 220 deletions.
13 changes: 1 addition & 12 deletions .gitignore
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
4 changes: 0 additions & 4 deletions .gitmodules

This file was deleted.

141 changes: 141 additions & 0 deletions bin/server.js
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)
11 changes: 11 additions & 0 deletions demo/index.html
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>
19 changes: 19 additions & 0 deletions demo/index.js
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 }
1 change: 0 additions & 1 deletion dist
Submodule dist deleted from 0f203f
40 changes: 0 additions & 40 deletions gulpfile.js

This file was deleted.

4 changes: 4 additions & 0 deletions now.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "y-webrtc",
"version": 1
}
Loading

0 comments on commit 1fac438

Please sign in to comment.