编程语言
首页 > 编程语言> > javascript-找不到GET http:// js:port / socket.io / 1/404

javascript-找不到GET http:// js:port / socket.io / 1/404

作者:互联网

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/>");
        });
    });

一开始我加载网站时就可以了.但是,一旦我单击聊天框,在“检查元素/控制台”中,我就会看到出现“ GET http://’js’:5001 / socket.io / 1 /?..”错误.请指教.

解决方法:

在服务器代码中,更改以下内容:

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'));
});

标签:node-js,socket-io,html,javascript
来源: https://codeday.me/bug/20191122/2058768.html