Javascript 我想基于URL创建多个名称空间,但我';m.io

Javascript 我想基于URL创建多个名称空间,但我';m.io,javascript,node.js,websocket,socket.io,namespaces,Javascript,Node.js,Websocket,Socket.io,Namespaces,用户可以通过点击localhost:3000/nsp1访问类似nsp1的命名空间 与nsp2相同,我希望使用从文本框中输入的名称动态创建它们 但现在我尝试使用下面的代码只创建一个名称空间,但服务器命中错误无法获取/my名称空间 server.js var io = require('socket.io')(http, { path: '/my-namespace'}); io .of('/my-namespace') .on('connection', function(socket){

用户可以通过点击localhost:3000/nsp1访问类似nsp1的命名空间 与nsp2相同,我希望使用从文本框中输入的名称动态创建它们 但现在我尝试使用下面的代码只创建一个名称空间,但服务器命中错误无法获取/my名称空间

server.js

var io  = require('socket.io')(http, { path: '/my-namespace'});

io
.of('/my-namespace')
.on('connection', function(socket){
    console.log('a user connected with id %s', socket.id);

    socket.on('my-message', function (data) {
        io.of('my-namespace').emit('my-message', data);
        // or socket.emit(...)
        console.log('broadcasting my-message', data);
    });
});
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(3000);
app.get("/contact.html", function (req, res) {
    res.sendfile(__dirname + '/contact.html');
});
app.get("/login.html", function (req, res) {
    res.sendfile(__dirname + '/login.html');
});
app.get("/", function (req, res) {
    res.sendfile(__dirname + '/home.html');
});
app.get(/(^\/[a-zA-Z0-9\-]+$)/, function (req, res) {
    //Matches anything with alphabets,numbers and hyphen without trailing slash
    res.sendfile(__dirname + '/room.html');
});

io.on('connection', function(socket){
    console.log('a user connected with id %s', socket.id);

    socket.on('join', function (room) {
        //May be do some authorization
        socket.join(room);
        console.log(socket.id, "joined", room);
    });
    socket.on('leave', function (room) {
        //May be do some authorization
        socket.leave(room);
        console.log(socket.id, "left", room);
    });
    socket.on('chat message', function (data) {
        //May be do some authorization
        io.to(data.room).emit("chat message", data.message);
    });
});
client.js

   var socket = io('localhost:3000/my-namespace', { path: '/my-namespace'});
server.js

var io  = require('socket.io')(http, { path: '/my-namespace'});

io
.of('/my-namespace')
.on('connection', function(socket){
    console.log('a user connected with id %s', socket.id);

    socket.on('my-message', function (data) {
        io.of('my-namespace').emit('my-message', data);
        // or socket.emit(...)
        console.log('broadcasting my-message', data);
    });
});
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(3000);
app.get("/contact.html", function (req, res) {
    res.sendfile(__dirname + '/contact.html');
});
app.get("/login.html", function (req, res) {
    res.sendfile(__dirname + '/login.html');
});
app.get("/", function (req, res) {
    res.sendfile(__dirname + '/home.html');
});
app.get(/(^\/[a-zA-Z0-9\-]+$)/, function (req, res) {
    //Matches anything with alphabets,numbers and hyphen without trailing slash
    res.sendfile(__dirname + '/room.html');
});

io.on('connection', function(socket){
    console.log('a user connected with id %s', socket.id);

    socket.on('join', function (room) {
        //May be do some authorization
        socket.join(room);
        console.log(socket.id, "joined", room);
    });
    socket.on('leave', function (room) {
        //May be do some authorization
        socket.leave(room);
        console.log(socket.id, "left", room);
    });
    socket.on('chat message', function (data) {
        //May be do some authorization
        io.to(data.room).emit("chat message", data.message);
    });
});
room.html/client.js

var socket = io('localhost:3000');
socket.emit("join",location.pathname);
socket.emit("chat message",{room:location.pathname, message:<val>});
var socket=io('localhost:3000');
socket.emit(“join”,location.pathname);
export.emit(“聊天消息”,{room:location.pathname,message:});

为此,你应该查看
房间
],同意梵天的观点。像这样动态的东西应该是房间,而不是名称空间。你能给我完整的代码吗,我在文档中尝试了该代码,但在@BrahmaDevPretty(与同一作者提出的问题相关):但我需要让用户通过在url中输入特定房间的名称,如localhost:3000/room1,然后是socket.join(“room1”)来进入特定房间我可以用socket.io或者我必须做一些代码@BrahmaYou不需要每个房间都有一个单独的名称空间。明白了。你需要像expressjs.com这样的东西。Socket.io不能这样做。@shrouk mansour你现在如何为前端服务?@jfriend00这要看情况了。也许不需要任何其他页面。只有/和通配符室的主页。我们试图解决所描述的问题,而不是构建一个我一无所知的产品。