1

好的,这就去。我制作了一个使用 Node.js 和 HTML5 Event-Streams 的简单聊天服务器,但由于某种原因,Event-Stream 无法工作,客户端页面在 textarea 中不显示任何应该保存来自 Event-Stream 的消息的内容. 我无法确定原因,我想知道是否有人可以帮助解决这个问题。

下面是我的 Javascript 文件:

请注意,另一个服务器文件采用传递给 /chat 的任何参数并将其作为 params 变量传递。例如:

/chat?room=testroom&nick=用户名&message=message1

将被解析为:

var params = { room : "testroom", nick : "username", message : "message1" };


聊天.js

var debug = require("./debug");

exports.rooms = { };

exports.chat = function(params, response)
{
    var rooms = this.rooms;
    if (params.room !== undefined)
    {
        if (rooms[params.room] === undefined)
        {   // create room
            debug.log("Creating room: " + params.room);
            rooms[params.room] = { };
            rooms[params.room].response = response;
            rooms[params.room].response.id = (new Date()).toLocaleTimeString();
            rooms[params.room].stack = { };
            rooms[params.room].stack.id = "[ROOM]";
            rooms[params.room].stack.message = "Welcome to " + params.room + "!";
            rooms[params.room].stackcount = 0;
            rooms[params.room].stream = function ()
            {
                    rooms[params.room].response.write("data: " + this.stack.id + ":" + this.stack.message + "\n\n");
                    debug.debug("[id|" + rooms[params.room].response.id + "]" + this.stack.id + ":" + this.stack.message);
            };
            debug.debug("Room created, sending: " + rooms[params.room].stack.id + ":" + rooms[params.room].stack.message);
            rooms[params.room].stream();
        }
        else
        {   // get room messages --or-- add message
            if (params.length == 1)
            {
                rooms[params.room].stream();
            }
            else
            {
                if (params.nick !== undefined && params.message !== undefined)
                {
                    //this.stackcount++;
                    rooms[params.room].stack.id = (new Date()).toLocaleTimeString();
                    rooms[params.room].stack.message = params.nick + ": " + params.message;
                    rooms[params.room].stream();
                }
            }
        }
    }
    else
    {
        var ret = "";
        ret = ret + '<html><head><script type="text/javascript">\
        function sendmsg()\
        {\
            var tmp = document.createElement("img");\
            tmp.src = "/chat?room=testroom&nick=" + document.getElementById("nick").value + "&message=" + document.getElementById("message").value;\
            return false;\
        }\
        </script>\
        </head><body>\
        <script>\
            var source = new EventSource("/chat?room=testroom");\
            source.onmessage = function(e)\
            {\
                document.getElementById("chatspace").innerHTML += e.data + "\\n";\
            };\
        </script>\
        <form action="javascript:sendmsg();" >\
            <input name=room type=hidden value="testroom"></input>\
            <input id=nick name=nick type=text placeholder="Your name" autocomplete=off><br />\
            <textarea id=chatspace rows=10 readonly></textarea><br />\
            <input id=message name=message type=text placeholder="Message" autocomplete=off><br />\
            <input type=submit value="Send"></input>\
        </form>\
        </body></html>';
        response.writeHead(200, {"Content-Type": "text/html"});
        response.write(ret);
        response.end();
    }
};

这是控制台输出:

[FATAL] Server up: 2012/8/21 @ 9:54:15
  [LOG] Request: /chat
  [LOG] Request: /chat?room=testroom
  [LOG] Creating room: testroom
[DEBUG] Room created, sending: [ROOM]:Welcome to testroom!
[DEBUG] [id|09:54:24][ROOM]:Welcome to testroom!
  [LOG] Request: /favicon.ico
  [LOG] Request: /chat?room=testroom&nick=username&message=message1
[DEBUG] [id|09:54:24]09:54:34:username: message1
  [LOG] Request: /chat?room=testroom&nick=username&message=message2
[DEBUG] [id|09:54:24]09:54:37:username: message2
  [LOG] Request: /chat?room=testroom&nick=username&message=message3
[DEBUG] [id|09:54:24]09:54:38:username: message3
4

0 回答 0