admin管理员组文章数量:1430059
I have a problem:
I use a room join system [which I programmed only in NodeJS] and socket.io.
/:roomid/
e.g. /
my route:
router.get('/room/:roomid/', function (req, res, next) {
var id = req.params.roomid;
//....
});
I want to make sockets in rooms, but how I make it with the namespace?
io.of("/room/:roomid/").on('connection', function (socket) {
io.of("/room/:roomid/").emit('testsocket');
}
-> does not work
client code:
var socketIO = io(window.location.pathname);
socketIO.on('testsocket', function (data) {
console.log("socket successful connected!");
});
I have a problem:
I use a room join system [which I programmed only in NodeJS] and socket.io.
https://example./room/:roomid/
e.g. https://example./room/764363553/
my route:
router.get('/room/:roomid/', function (req, res, next) {
var id = req.params.roomid;
//....
});
I want to make sockets in rooms, but how I make it with the namespace?
io.of("/room/:roomid/").on('connection', function (socket) {
io.of("/room/:roomid/").emit('testsocket');
}
-> does not work
client code:
var socketIO = io(window.location.pathname);
socketIO.on('testsocket', function (data) {
console.log("socket successful connected!");
});
Share
Improve this question
edited Apr 15, 2018 at 13:06
Freddy C.
asked Apr 15, 2018 at 10:52
Freddy C.Freddy C.
1292 silver badges11 bronze badges
6
-
What do you mean by
does not work
? Does it throw an error ? – user2652134 Commented Apr 15, 2018 at 11:27 - @BrahmaDev I mean: the socket does not connect. – Freddy C. Commented Apr 15, 2018 at 11:56
- Can you then also add the code you use to connect. – user2652134 Commented Apr 15, 2018 at 11:57
- @BrahmaDev Yes, but if I emit something it does not arrive. – Freddy C. Commented Apr 15, 2018 at 12:09
- Please, add the code here. I cannot debug things that I can't see. Meanwhile you're telling me contradicting points. – user2652134 Commented Apr 15, 2018 at 12:58
2 Answers
Reset to default 4Socketio supports Dynamic namespace since 2.1.0.
The pr is here.
document is here:
A regex or a function can also be provided, in order to create namespace in a dynamic way:
const dynamicNsp = io.of(/^\/dynamic-\d+$/).on('connect', (socket) => {
const newNamespace = socket.nsp; // newNamespace.name === '/dynamic-101'
// broadcast to all clients in the given sub-namespace
newNamespace.emit('hello');
});
// client-side
const socket = io('/dynamic-101');
// broadcast to all clients in each sub-namespace
dynamicNsp.emit('hello');
// use a middleware for each sub-namespace
dynamicNsp.use((socket, next) => { /* ... */ });
With a function:
io.of((name, query, next) => {
next(null, checkToken(query.token));
}).on('connect', (socket) => { /* ... */ });
Server
var manager = io.of("/room").on('connection', function (socket) {
socket.on("join", function(roomid){
socket.join(roomid);
manager.to(roomid).emit('testsocket',roomid);
}
}
Client:
var socketIO = io("/room");
var roomID = window.location.pathname.splitOnLast("/")[1]; //Should ideally be got from req.params.roomid
socketIO.emit("join", roomID)
socketIO.on('testsocket', function (data) {
console.log("Connected to room", data);
});
本文标签: javascriptSocketio namespace with variableid in routeStack Overflow
版权声明:本文标题:javascript - Socket.io namespace with variableid in route - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745548778a2662832.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论