Created
July 14, 2019 05:16
-
-
Save vnnvanhuong/182dcf90ba05de227d1819abab1b014f to your computer and use it in GitHub Desktop.
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
// DDP Server | |
// Use an implementation of WebSocket Server such as SockJS | |
ddpServer.socket = WebSocketServer | |
ddpServer.socket.onData = (msg) => { | |
switch(msg): | |
case 'connect': | |
check whether WebSocket connection | |
socket.send({msg: 'connected', ...}) | |
case 'method': | |
result = methodsMap.get(msg.methodID).invoke() | |
socket.send({'result': ...}) | |
case 'sub': | |
publicationsMap.get(msg.subId).push(new Subscription) | |
socket.send({msg: 'ready', ...}) | |
case ... | |
} | |
ddpServer.publish = (name) => { | |
publicationsMap.push(name) | |
} | |
dppServer.methods = (names) => { | |
methodsMap.push(names) | |
} | |
// DDP Client | |
// Simply use a WebSocket client to open connection | |
// and send messages to the WebSocket Server | |
ddpClient.ws = WebSocket | |
ddpClient.connect = () => { | |
ws.on('open', () => ws.send(EJSON({msg: 'connect', ...}))) | |
} | |
ddpClient.call = () => { | |
ws.send(EJSON({msg: 'method', ...})) | |
} | |
ddpClient.subscribe = () => { | |
ws.send(EJSON({msg: 'sub', ...})) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment