在使用socket.io的聊天室中获取错误

时间:2016-11-01 23:14:26

标签: socket.io

我使用的代码如下

server.js

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
users = [];
connections = [];

server.listen(process.env.PORT || 3000);

console.log('Server Running...');

app.get('/',function (req, res)
{
    res.sendFile(__dirname + '/index.html');
});

io.sockets.on('connection', function(socket){
    connections.push(socket);
    console.log('Connected to %s sockets connection', connections.length);

    // Disconnect
    socket.on('disconnect', function (data){
        if(!socket.username) return;
        users.splice(users.indexOf(socket.username), 1);
        updateUsernames();
        connections.splice(connections.indexOf(socket), 1);
        console.log('Disconnected: %s socket connected', connections.length);
    });

    //Send Message 
    socket.on('send message', function(data){
        console.log(data);
        io.sockets.emit('new message', {msg: data});
    });

    // New user
    socket.on('new user', function(data, callback){
        callback(true);
        socket.username = data;
        console.log(socket.username);
        users.push(socket.username);
        updateUsernames();
    });

    function updateUsernames(){
        io.sockets.emit('get users', users);
    }

});

index.html - >客户端

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="http://immodernafrican.move.pk:3000/socket.io/socket.io.js"></script>
    <style type="text/css">
        body{
            margin-top: 30px;
        }
        #messageArea{
            display: none;  
            }

    </style>
</head>
<body>
<div class="container">

    <div id="userFormArea" class="row">
        <div class="col-md-12">
            <form id="userForm">
                <div class="form-group">
                    <label>Enter Username</label>
                    <input class="form-control" id="username"/>
                    <br/>
                    <input type="submit" class="btn btn-primary " value="Login">
                </div>
            </form>
        </div>
    </div>

    <div class="row" id="messageArea">
        <div class="col-md-4">
            <div class="well">
                <h3>Online Users</h3>
                <ul class="list-group" id="users"></ul>
            </div>
        </div>
        <div class="col-md-8">
            <div class="chat" id="chat"></div>
            <form id="messageForm">
                <div class="form-group">
                    <label>Enter Message</label>
                    <textarea class="form-control" id="message"></textarea>
                    <br/>
                    <input type="submit" class="btn btn-primary " value="Submit">
                </div>
            </form>
        </div>
    </div>

</div>
<script type="text/javascript">

        $(function(){
            var socket = io.connect('http://immodernafrican.move.pk:3000');
            var $messageForm = $('#messageForm');
            var $message = $('#message');
            var $chat = $('#chat');
            var $messageArea = $('#messageArea');
            var $userFormArea = $('#userFormArea');
            var $userForm = $('#userForm');
            var $users = $('#users');
            var $username = $('#username');


            $messageForm.submit(function(e){
                e.preventDefault();
                socket.emit('send message', $message.val());
                $message.val('');
            }); 

            socket.on('new message', function(data){
                $chat.append('<div class="well">'+data.msg+'</div>')
            });

            $userForm.submit(function(e){
                e.preventDefault();
                socket.emit('new user', $username.val(), function(data){
                    if(data){
                        $userFormArea.hide();
                        $messageArea.show();
                    }
                });
                $username.val('');
            }); 

            socket.on('get users', function(data){
                var html = '';
                for(i=0; i<data.length; i++)
                {
                    html += '<li class="list-group-item">'+data[i]+'</li>';
                }
                users.html(html);
            });



        });


</script>
</body>
</html>

这在localhost上运行准确,但只要我将其上传到在线托管,就会出错。这些是我在控制台中看到的错误

(index):7 GET http://immodernafrican.move.pk:3000/socket.io/socket.io.js net::ERR_CONNECTION_TIMED_OUT
(index):58 Uncaught ReferenceError: io is not defined
    at HTMLDocument.<anonymous> (http://immodernafrican.move.pk/:58:17)
    at n (https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:2:14784)
    at Object.fireWith (https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:2:15553)
    at Function.ready (https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:2:9773)
    at HTMLDocument.B (https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:2:14348)

我试图改变

var socket = io.connect('http://immodernafrican.move.pk:3000');

var socket = io.connect();

但是任何人都可以帮助我解决这个问题,这样我的聊天室就可以了 感谢

0 个答案:

没有答案