简体   繁体   English

Socket.IO名称空间不起作用

[英]Socket.IO namespaces won't work

I am trying to make a socket.io node chat room application 我正在尝试制作一个socket.io节点聊天室应用程序
And my dependices are 我的家属是
socket.io@1.4.5 socket.io@1.4.5
node@v5.7.0 At client side I added node@v5.7.0在客户端,我添加了

window.onload = function(){

        + var socket = io.connect('http://localhost:4000/chat');
        var messagesElement = document.getElementById('messages');
        var lastMessageElement = null;

And the server side I added 我添加的服务器端

- io.sockets.on('connection',function(socket){
   + io.of('/chat').on('connection',function(socket){

Before those changes the application works fine 在进行这些更改之前,应用程序可以正常运行
When I am trying to use socket.io namespaces to 当我尝试使用socket.io命名空间来
separate different applications on the server and on the client. 在服务器和客户端上分离不同的应用程序。
It doesn't work and I don't find any error or bug on the console. 它不起作用,在控制台上找不到任何错误或错误。

My application's repository link 我的应用程序的存储库链接

I just found there is a same variable below the socket variable in the client side 我只是发现客户端的套接字变量下面有一个相同的变量

        <script>
window.onload = function(){
      + var socket = io.connect('http://localhost:4000/chat');
      var socket = io.connect('/chat');
            var messagesElement = document.getElementById('messages');
            var lastMessageElement = null;

@@ -36,7 +36,7 @@ window.onload = function(){
                lastMessageElement=newMessageElement;
            }
    here--> -var socket = io.connect('http://localhost:4000');
   +//var socket = io.connect('http://localhost:4000');
        socket.on('serverMessage',function(content){
            addMessage(content);
        });

And I comment it, it works now. 我对此进行评论,现在可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM