Make pubsub example more interesting
This commit is contained in:
parent
8d7527520f
commit
ce62261986
@ -13,22 +13,40 @@ const app = uWS./*SSL*/App({
|
|||||||
maxPayloadLength: 16 * 1024 * 1024,
|
maxPayloadLength: 16 * 1024 * 1024,
|
||||||
idleTimeout: 10,
|
idleTimeout: 10,
|
||||||
// would be nice to have maxBackpressure to automatically close slow receivers
|
// would be nice to have maxBackpressure to automatically close slow receivers
|
||||||
|
|
||||||
|
/* Setting 1: merge messages in one, or keep them as separate WebSocket frames - mergePublishedMessages */
|
||||||
|
/* Setting 2: compression on/off - cannot have dedicated compressor for pubsub yet */
|
||||||
|
/* Setting 3: maxBackpressure - when we want to automatically terminate a slow receiver */
|
||||||
|
/* Setting 4: send to all including us, or not? That's not a setting really just use ws.publish or global uWS.publish */
|
||||||
|
|
||||||
/* Handlers */
|
/* Handlers */
|
||||||
open: (ws, req) => {
|
open: (ws, req) => {
|
||||||
/* Let all new sockets subscribe to chat topic */
|
/* Let this client listen to all sensor topics */
|
||||||
ws.subscribe('chat');
|
ws.subscribe('home/sensors/#');
|
||||||
},
|
},
|
||||||
message: (ws, message, isBinary) => {
|
message: (ws, message, isBinary) => {
|
||||||
console.log('Got WebSocket message!');
|
/* Parse this message according to some application
|
||||||
/* If we get a message from any socket, we publish it to the chat topic */
|
* protocol such as JSON [action, topic, message] */
|
||||||
ws.publish('chat', message);
|
|
||||||
|
/* Let's pretend this was a temperature message
|
||||||
|
* [pub, home/sensors/temperature, message] */
|
||||||
|
ws.publish('home/sensors/temperature', message);
|
||||||
|
|
||||||
|
/* Let's also pretend this was a light message
|
||||||
|
* [pub, home/sensors/light, message] */
|
||||||
|
ws.publish('home/sensors/light', message);
|
||||||
|
|
||||||
|
/* If you have good imagination you can also
|
||||||
|
* pretend some message was a subscription
|
||||||
|
* like so: [sub, /home/sensors/humidity].
|
||||||
|
* I hope you get how any application protocol
|
||||||
|
* can be implemented with these helpers. */
|
||||||
},
|
},
|
||||||
drain: (ws) => {
|
drain: (ws) => {
|
||||||
|
|
||||||
},
|
},
|
||||||
close: (ws, code, message) => {
|
close: (ws, code, message) => {
|
||||||
// here we need to properly unsubscribe
|
/* The library guarantees proper unsubscription at close */
|
||||||
// ws.unsubscribe('*');
|
|
||||||
}
|
}
|
||||||
}).any('/*', (res, req) => {
|
}).any('/*', (res, req) => {
|
||||||
res.end('Nothing to see here!');
|
res.end('Nothing to see here!');
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 5528e6f5fec2ffbd8e684e5d7d966eb0ae2bd5d5
|
Subproject commit 6fe717f46a0f4188e1fc929776d1b86791ec7cb0
|
Loading…
Reference in New Issue
Block a user