-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Forwarding communication #1715
Comments
A colleague figured out a slightly more elegant way. This method creates a actual 'any' event and allows you to work with regular event listeners. var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
http.listen(3000);
var subject = null;
var nsp = io.of("/");
nsp.on('connection', function(socket){
if(socket.handshake.query.isSubject) {
subject = socket;
}
createAnyEvents(socket);
socket.on('any', function (eventType,data,callback) {
if(socket === subject) {
// forward all events from subject to other clients in namespace
subject.broadcast.emit(eventType,data);
} else {
// forward all events from clients to subject
subject.emit(eventType,data,callback);
}
});
});
function createAnyEvents(socket) {
var originalOnEvent = socket.onevent;
socket.onevent = function() {
// emit regular event
originalOnEvent.apply(socket, arguments);
var data = arguments[0].data;
/** Note: turn this event into a 'any' event
* We add the event type as first argument, the regular arguments
* (data and callback) become the subsequent arguments
*/
data.unshift('any');
// emit 'any' event
originalOnEvent.apply(socket, arguments);
};
} |
The following package also seems interesting, but it's probably not compatible with socket.io 1.0. It tries to override onClientMessage in a Manager, but this Manager doesn't seem to exist anymore. |
There is also https://npmjs.org/package/socket.io-events which gives you more of an express style routing of events, it supports wild cards and regular expressions. |
I'd like to forward websocket communication (custom events, messages etc). I have to forward communication between clients because they can't reach each other directly (because they are usually behind routers etc).
Is there any way to handle any event, so I don't have to specify all the event names on my server?
I would really like to do is the following:
Looking through the code I found the onevent handler, and inspired by #1185 I figured out the following solution. My question is, is there a better solution?
(If there isn't I hope the solution is useful more people)
This handles all events, to go even more generic you could override
onpacket
.The text was updated successfully, but these errors were encountered: