2

我正在为我的网站开发一个实时聊天网络应用程序。我的服务器的行为与我需要的行为方式略有不同。我有一个 node.js 不兼容的 Apache PHP 服务器。我的节点服务器是公共的,但要连接,我必须从服务器命名客户端的目录,这意味着当我打开服务器IP 时,它将显示客户端。我想将客户端上传到另一个托管服务器上并从那里连接到我的 Socket.IO 和节点服务器。这是我的代码。
客户

<!DOCTYPE html>
<html lang='en'>
    <head>
        <title>Chat</title>
        <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js'></script>

        <script src='node_modules/socket.io/socket.io.js'></script>
        <script>
                var socket = new io.Socket("127.0.0.1", {port:911});
                socket.connect();
                //Once connected
                socket.on('welcome', function(data) {
                    $('#log ul').append('<li>' + data.salutation + '</li>');
                });
                //Displayig the data from the server
                socket.on('data from server', function(data) {
                    $('#log ul').append('<li>' + data.message + '</li>');
                });


                $(function() {
                    var entry_el = $('#entry');
                    $('#entry').keypress(function(event) {
                        //13 on your keyboard is the key enter
                        if(event.keyCode == 13) {
                            var msg = entry_el.attr('value');
                            if(msg) {
                                socket.emit('data from client', {'text': msg});
                                entry_el.attr('value', '');
                                window.scrollBy(0, 1000000000000);
                                entry_el.focus();
                            }
                        }
                    });
                });
            /*SCRIPT DONE
                CSS START
                */
        </script>

        <style type='text/css'>
            body {
                color: fff;
                font-size: 14px;
                margin: 0;
                padding: 0;
                font-family: Helvetica, Arial, Sans-Serif;
            }
            #log {
                margin-bottom: 100px;
                width: 100%;
            }
            #log ul {
                padding: 0;
                margin: 0;
            }
            #log ul li {
                list-style-type: none;
            }
            #console {
                position: fixed;
                width: 100%;
                font-size: 18px;
            }
        </style>
    </head>
    <body>
        <h1>Chat</h1>
        <div id='log'><ul></ul></div>
        <div id='console'>
            <input type='text' id='entry'/>
        </div>
    </body>
</html>

服务器

var http = require('http').createServer(handler);
var io = require('socket.io').listen(http);
var sys = require('sys');
var fs = require('fs');

var clients = [];

http.listen(911);

function handler(request, response) {
    //If the response is Positive (200)
    response.writeHead(200, {
        'Content-Type':'text/html'
    });
    sys.pump(rs, response);
};

//connecting to sockets
io.sockets.on('connection', function(socket) {

    var username;

    clients.push(socket);
    socket.emit('welcome', {'salutation':'Welcome to this chat server!'});
    socket.emit('welcome', {'salutation':'Please input your username:'});

    socket.on('data from client', function(data) {
        console.log('message: ' + data.text);
        if(!username) {
            username = data.text;
            socket.emit('data from server', {'message': 'Welcome, ' + username + '!'});
            return;
        }
        //the response when someone types something
        var feedback = username + ' said: ' + data.text;
        clients.forEach(function(socket) {
            socket.emit('data from server', {'message': feedback});
        });
    });

});
4

0 回答 0