Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message handling #53

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "y-websocket",
"version": "1.3.11",
"version": "1.3.12",
"description": "Websockets provider for Yjs",
"main": "./dist/y-websocket.cjs",
"module": "./src/y-websocket.js",
Expand Down
25 changes: 23 additions & 2 deletions src/y-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ const setupWS = provider => {

websocket.onmessage = event => {
provider.wsLastMessageReceived = time.getUnixTime()
const encoder = readMessage(provider, new Uint8Array(event.data), true)
let _event = event
provider.eventPreProcess(_event)
const encoder = readMessage(provider, _event.data, true)
if (encoding.length(encoder) > 1) {
websocket.send(encoding.toUint8Array(encoder))
}
Expand Down Expand Up @@ -166,6 +168,16 @@ const broadcastMessage = (provider, buf) => {
}
}

/**
* @function
* @param {object} event
* @return {object}
*/
const eventPreProcess = (event) => {
event.data = new Uint8Array(event.data)
return event
}

/**
* Websocket Provider for Yjs. Creates a websocket connection to sync the shared document.
* The document name is attached to the provided url. I.e. the following example
Expand All @@ -190,8 +202,16 @@ export class WebsocketProvider extends Observable {
* @param {Object<string,string>} [opts.params]
* @param {typeof WebSocket} [opts.WebSocketPolyfill] Optionall provide a WebSocket polyfill
* @param {number} [opts.resyncInterval] Request server state every `resyncInterval` milliseconds
* @param {eventPreProcess} [opts.customEventPreProcess] Optional pre-processing on incomeing messages
*/
constructor (serverUrl, roomname, doc, { connect = true, awareness = new awarenessProtocol.Awareness(doc), params = {}, WebSocketPolyfill = WebSocket, resyncInterval = -1 } = {}) {
constructor (serverUrl, roomname, doc, {
connect = true,
awareness = new awarenessProtocol.Awareness(doc),
params = {},
WebSocketPolyfill = WebSocket,
resyncInterval = -1,
customEventPreProcess = eventPreProcess
} = {}) {
super()
// ensure that url is always ends with /
while (serverUrl[serverUrl.length - 1] === '/') {
Expand All @@ -210,6 +230,7 @@ export class WebsocketProvider extends Observable {
this.wsUnsuccessfulReconnects = 0
this.messageHandlers = messageHandlers.slice()
this.mux = mutex.createMutex()
this.eventPreProcess = customEventPreProcess || eventPreProcess
/**
* @type {boolean}
*/
Expand Down