简体   繁体   中英

sockJS own event names

I am using sockjs-node and I want to emit on my own events(using conn.write(...) i am able only to emit on 'data') is it possible to do so? And how?

echo.on('connection', function(conn) {
conn.emit('message', {msg:'xd'}); //I know that it doesnt work, just i would like to know is it possible to achieve emitting on my own events using sockjs
conn.on('close', function() {
    console.log('close');
});
conn.end();
});

for example, sth similar to the line with conn.emit

sockJS works like a webSocket where it just delivers data packets to you. If you want a messaging layer on top of that, you could build one fairly easily, but it is not part of sockJS itself.

To build your own message layer, you could just built a simple abstraction on top of the sockJS transport that supports .emit(msg, data) and then sends a message over sockJS that has two parts to it, the message name and the data, perhaps converted to JSON for sending the JSON over sockJS.

On the receiving end, you would do just the opposite and you could have your own eventEmitter so you'd receive the sockJS data, break it apart into message name and data and then emit the data to the message name.

FYI, it might just make sense to use socket.io since it is already a messaging layer on top of webSocket (and a few other features such as auto-reconnect).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM