综合技术

Accessing socket.io scope from outer scope

微信扫一扫,分享到朋友圈

Accessing socket.io scope from outer scope
0

I have a scenario where user sends a POST request, in express I set up a route, get the post data, validate it, store it in db then I need to broadcast it to all sockets connected. But since the socket variable is available inside io.sockets.on('connection')
I might be able to access the inner scope of it with a closure this way:

Var a = {}; //global
io.sockets.on('connection',function(socket){
    a.inner = function(data){
        socket.broadcast.emit('boo',data)
    }
}

Then call a.inner(data)

Is this normal? is there a better way to access or call inner functions ?

Thanks

Problem courtesy of: Rana Deep

阅读原文...

微信扫一扫,分享到朋友圈

Accessing socket.io scope from outer scope
0
Node.js Recipes

Rewrite HTTP headers with Azure Application Gateway

上一篇

After having checked our privilege, how shall we continue?

下一篇

评论已经被关闭。

插入图片

热门分类

往期推荐

Accessing socket.io scope from outer scope

长按储存图像,分享给朋友