使用 TCP 的 Net Stream 对象效果很好(作为 node.js introduction video 中的预设),但我应该如何在 HTTP 中执行此操作?
有没有办法在 http.createServer()
中访问套接字/客户端?或者有什么方法可以做到?我试图从官方node chat demos中找出解决方案souce code ,但我真的不明白。
我了解客户端 js,但是在我(作为客户端)通过 AJAX 向服务器端 js 发送消息后会发生什么?如何发送给服务器上的其他客户端?
请注意,我不想学习流程的逻辑,所以我不想使用 socket.io 或任何其他框架、库、模块。
非常感谢您的帮助!
最佳答案
理想情况下,您只需使用 WebSockets
但替代方法是 ajax 长轮询。
您可以使用一种称为长轮询的技术进行聊天。这意味着您向服务器发出(ajax)请求,并且服务器保持该请求直到它有一些数据要发送。
所以客户端最终会定期轮询服务器,如果服务器没有新消息,它只会保留您的请求。如果它有消息,它会将其发送回客户端,客户端将再次轮询服务器。
[[伪代码]]
//客户端.js
var Socket = function(ip, port, name) {
this.ip = ip;
this.port = port;
this.name = name;
this._cbs = [];
this._poll();
};
// Call the server periodically for data.
Socket.prototype._poll = function() {
var that = this;
// if the server does not return then call it again
var timer = setTimeout(function() {
this._poll();
}, 5000);
$.ajax({
type: "GET",
timeout: 5000,
data: {
name: this.name
},
url: this.ip + ":" + this.port,
success: function(data) {
// server returned, kill the timer.
clearTimeout(timer);
// send the message to the callback.
for (var i = 0; i < that._cbs.length; i++) {
that._cbs[i](data);
}
// call the server again
that._poll();
}
});
};
// Add a callback for a message event
Socket.prototype.on = function(event, cb) {
if (event === "message") {
this._cbs.push(cb);
}
};
// Send a message to the server
Socket.prototype.send = function(message) {
$.ajax({
data: {
message: message,
name: this.name
},
type: "GET",
url: this.ip + ":" + this.port
});
};
var socket = new Socket('192.168.1.1', '8081', "Raynos");
socket.on("message", function(data) {
console.log(data);
});
socket.send("Hello world!");
//server.js
var url = require("url");
var events = require("events");
// store messages for clients
var clients = {};
var emitter = new events.EventEmitter();
http.createServer(function(req, res) {
// get query string data
var data = url.parse(req.url, true).query;
// if client is not initialized then initialize it.
if (data.name && !clients[data.name]) {
clients[data.name] = [];
}
// if you posted a message then add it to all arrays
if (data.message) {
for (var k in clients) {
clients[k].push(data.name + " : " + data.message);
}
// tell long pollers to flush new data.
emitter.emit("new-data");
} else if (clients[data.name].length > 0) {
// else empty the clients array down the stream
for (var i = 0; i < clients[data.name].length; i++) {
res.write(clients[data.name].shift());
};
res.end();
// long polling magic.
} else {
var cb = function() {
for (var i = 0; i < clients[data.name].length; i++) {
res.write(clients[data.name].shift());
};
res.end();
// kill that timer for the response timing out.
clearTimeout(timer);
}
// when we get data flush it to client
emitter.once("new-data", cb);
var timer = setTimeout(function() {
// too long has passed so remove listener and end response.
emitter.removeListener(cb);
res.end();
}, 4500);
}
}).listen(8081);
更好的推送技术是Server-side events .见 example of it here .不过,这确实需要浏览器支持(我认为是 Chrome 和 opera)。
关于javascript - Node.JS:如何创建 HTTP 聊天服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6052181/