繁体   English   中英

Socket.io 无法在网络中工作

[英]Socket.io not working in the net

我使用 socket.io 创建了一个简单的聊天室。 我的 index.html 中有这些脚本:

var socket = io.connect('http://imageworkz.asia:8080');

    // on connection to server, ask for user's name with an anonymous callback
    socket.on('connect', function(){
        // call the server-side function 'adduser' and send one parameter (value of prompt)
        socket.emit('adduser', prompt("What's your name?"));
    });

    // listener, whenever the server emits 'updatechat', this updates the chat body
    socket.on('updatechat', function (username, data) {
        $('#conversation').append('<b>'+username + ':</b> ' + data + '<br>');
    });

    // listener, whenever the server emits 'updateusers', this updates the username list
    socket.on('updateusers', function(data) {
        $('#users').empty();
        $.each(data, function(key, value) {
            $('#users').append('<div>' + key + '</div>');
        });
    });

    // on load of page
    $(function(){
        // when the client clicks SEND
        $('#datasend').click( function() {
            var message = $('#data').val();
            $('#data').val('');
            // tell server to execute 'sendchat' and send along one parameter
            socket.emit('sendchat', message);
        });

        // when the client hits ENTER on their keyboard
        $('#data').keypress(function(e) {
            if(e.which == 13) {
                $(this).blur();
                $('#datasend').focus().click();
            }
        });
    });

当我将连接更改为http://localhost:8080并在控制台中使用“node app.js”命令启动它时,它工作正常,但是当我上传它并将其更改为http://imageworkz.asia:8080 时,它每当我访问 url: http://imageworkz.asia:8080时都不起作用。 我是否遗漏了某些东西,或者我还需要做些什么才能让它在上传时正常工作? 还是我去错了网址? 谢谢!

尝试将您的 node.js 版本更新到网络上的最新版本 (http://imageworkz.asia:8080)。

还要检查网络上是否安装了所有必需的节点模块,如果需要,更改逻辑,以便不需要 prompt() 来传输消息。

我不完全确定,但我认为这应该有效:

var socket = io.connect('http://imageworkz.asia');

    // on connection to server, ask for user's name with an anonymous callback
    socket.on('connect', function(){
        // call the server-side function 'adduser' and send one parameter (value of prompt)
        socket.emit('adduser', prompt("What's your name?"));
    });

    // listener, whenever the server emits 'updatechat', this updates the chat body
    socket.on('updatechat', function (username, data) {
        $('#conversation').append(''+username + ': ' + data + '
'); }); // listener, whenever the server emits 'updateusers', this updates the username list socket.on('updateusers', function(data) { $('#users').empty(); $.each(data, function(key, value) { $('#users').append('' + key + ''); }); }); // on load of page $(function(){ // when the client clicks SEND $('#datasend').click( function() { var message = $('#data').val(); $('#data').val(''); // tell server to execute 'sendchat' and send along one parameter socket.emit('sendchat', message); }); // when the client hits ENTER on their keyboard $('#data').keypress(function(e) { if(e.which == 13) { $(this).blur(); $('#datasend').focus().click(); } }); });

它只是删除 :8080

暂无
暂无

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

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