GET http:// js:port / socket.io / 1/440未找到

时间:2013-12-29 01:57:58

标签: javascript html node.js socket.io

Socket.io正在连接问题。

这是我的服务器代码:

function chatserver(){

var express = require('express'),

app = express(),

server = require('http').createServer(app).listen(app.get('port'),function(){

        console.log('Express server listening on port '+ app.get('port'));
    });
var io = require('socket.io').listen(server),
    users = {};

io.sockets.on('connection', function(socket){
    socket.on('new user', function(data, callback){
        if (data in users){
            callback(false);
        } else{
            callback(true);
            socket.nickname = data;
            users[socket.nickname] = socket;
            updateNicknames();
        }
    });

    function updateNicknames(){
        io.sockets.emit('usernames', Object.keys(users));
    }

    socket.on('send message', function(data, callback){
        var msg = data.trim();
        console.log('after trimming message is: ' + msg);
        if(msg.substr(0,3) === '/w '){
            msg = msg.substr(3);
            var ind = msg.indexOf(' ');
            if(ind !== -1){
                var name = msg.substring(0, ind);
                var msg = msg.substring(ind + 1);
                if(name in users){
                    users[name].emit('whisper', {msg: msg, nick: socket.nickname});
                    console.log('message sent is: ' + msg);
                    console.log('Whisper!');
                } else{
                    callback('Error!  Enter a valid user.');
                }
            } else{
                callback('Error!  Please enter a message for your whisper.');
            }
        } else{
            io.sockets.emit('new message', {msg: msg, nick: socket.nickname});
        }
    });

    socket.on('disconnect', function(data){
        if(!socket.nickname) return;
        delete users[socket.nickname];
        updateNicknames();
    });
});
}

我的HTML脚本代码:

jQuery(function($){

        var socket = io.connect('js/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js');

        var $nickForm = $('#setNick');
        var $nickError = $('#nickError');
        var $nickBox = $('#nickname');
        var $users = $('#users');
        var $messageForm = $('#send-message');
        var $messageBox = $('#message');
        var $chat = $('#chat');

        $nickForm.submit(function(e){
            e.preventDefault();
            socket.emit('new user', $nickBox.val(), function(data){
                if(data){
                    $('#nickWrap').hide();
                    $('#contentWrap').show();
                } else{
                    $nickError.html('That username is already taken!  Try again.');
                }
            });
            $nickBox.val('');
        });

        socket.on('usernames', function(data){
            var html = '';
            for(i=0; i < data.length; i++){
                html += data[i] + '<br/>'
            }
            $users.html(html);
        });

        $messageForm.submit(function(e){
            e.preventDefault();
            socket.emit('send message', $messageBox.val(), function(data){
                $chat.append('<span class="error">' + data + "</span><br/>");
            });
            $messageBox.val('');
        });

        socket.on('new message', function(data){
            $chat.append('<span class="msg"><b>' + data.nick + ': </b>' + data.msg + "</span><br/>");
        });

        socket.on('whisper', function(data){
            $chat.append('<span class="whisper"><b>' + data.nick + ': </b>' + data.msg + "</span><br/>");
        });
    });

当我首先加载网站时,没关系。但是当我单击聊天框时,在inspect elements / console中我会看到"GET http://'js':5001/socket.io/1/?.."错误。请指教。

2 个答案:

答案 0 :(得分:3)

在服务器代码中,更改此:

server = require('http').createServer(app).listen(port),

io = require('socket.io').listen(server),

var server = http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

var io = require('socket.io').listen(server);

如果你在http服务器而不是express.io服务器,请使用:

app.listen(app.get('port'), function(){
   console.log("Express server listening on port " + app.get('port'));
});

答案 1 :(得分:0)

您的主要问题是客户端Jquery脚本。

  

var socket = io.connect(&#39; js / node_modules / socket.io / node_modules / socket.io-client / dist / socket.io.js&#39;);

这不是将socket.io.js服务器提供给客户端的正确方法。 您需要将其更改为http://<ip/domain>:<port>/socket.io/socket.io.js

如果Socket.io服务器正确地侦听您的HTTP服务器,它将自动通过http://<ip/domain>:<port>/socket.io/socket.io.js向客户端文件提供服务。您无需在resources/js/socket.io.js&amp;手动提供。

代码示例
Express 3.x - Express 3要求您实例化http.Server以将socket.io附加到第一个

var express = require('express')
  , http = require('http');
//make sure you keep this order
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);

//... 

server.listen(8000);

快乐编码:)