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);
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745548779a4632466.html
评论列表(0条)