开发者

How to define a new function for a socket object in socket.io

Say I have

io.sockets.some_custom_function = function () { console.log('blah'); }
//Above doesn't work

io.sockets.on('connection', function (sock开发者_如何学Cet) {
    socket.some_custom_function(); //Error, obviously
});

How do I declare a new function for the socket object?


Not that I'm questioning what you're doing, but perhaps there is a better way to do what you want. What is your reasoning for creating a new function for the socket object?

By the way, I think sockets and socket are different things so adding something to one wouldn't modify the socket you're passed in the callback of an event.

You might be better off creating a function that accepts the socket, or a socket's parameters. That way you wont be held responsible for knowing the way a socket is constructed, which may be subject to change at any time. You'd also be free of any potential future bugs, if your injected code and their code overlap or shadow each other later on.

If you're still determined, on github you can see the socket.js file which represents the actual socket you want to modify. You'd have to find a reference to it and then add the function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜