-
Notifications
You must be signed in to change notification settings - Fork 451
/
client.js
42 lines (39 loc) · 1.41 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var ReconnectingWebSocket = require('reconnecting-websocket');
var sharedb = require('sharedb/lib/client');
var richText = require('rich-text');
var Quill = require('quill');
sharedb.types.register(richText.type);
// Open WebSocket connection to ShareDB server
var socket = new ReconnectingWebSocket('ws://' + window.location.host, [], {
// ShareDB handles dropped messages, and buffering them while the socket
// is closed has undefined behavior
maxEnqueuedMessages: 0
});
var connection = new sharedb.Connection(socket);
// For testing reconnection
window.disconnect = function() {
connection.close();
};
window.connect = function() {
var socket = new ReconnectingWebSocket('ws://' + window.location.host, [], {
// ShareDB handles dropped messages, and buffering them while the socket
// is closed has undefined behavior
maxEnqueuedMessages: 0
});
connection.bindToSocket(socket);
};
// Create local Doc instance mapped to 'examples' collection document with id 'richtext'
var doc = connection.get('examples', 'richtext');
doc.subscribe(function(err) {
if (err) throw err;
var quill = new Quill('#editor', {theme: 'snow'});
quill.setContents(doc.data);
quill.on('text-change', function(delta, oldDelta, source) {
if (source !== 'user') return;
doc.submitOp(delta, {source: quill});
});
doc.on('op', function(op, source) {
if (source === quill) return;
quill.updateContents(op);
});
});