【发布时间】:2012-08-06 04:17:12
【问题描述】:
我正在学习 Node 并且有一个问题我似乎找不到答案。在以下简约聊天服务器示例中,节点服务器希望客户端页面与服务器文件位于同一目录中,如果我正在为移动设备构建客户端应用程序,我将如何将数据发送回正确的客户?
var fs = require('fs')
, http = require('http')
, socketio = require('socket.io');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-type': 'text/html'});
**res.end(fs.readFileSync(__dirname + '/index.html'));**
}).listen(8080, function() {
console.log('Listening at: http://localhost:8080');
});
socketio.listen(server).on('connection', function (socket) {
socket.on('message', function (msg) {
console.log('Message Received: ', msg);
socket.broadcast.emit('message', msg);
});
});
【问题讨论】:
-
您能否详细说明您的服务器和客户端应用程序之间的通信是如何工作的?我不确定我是否理解以下内容:
if I was building a client side app for a mobile device, how would I send the data back to the proper client -
对不起。我的意思是如果我是客户端 index.html 是在 Phonegap 中编译并驻留在个人手机上的本机应用程序,我如何将消息路由回客户端? res.end(fs.readFileSync(__dirname + '/index.html'));是 index.html 是否在节点服务器上
标签: javascript node.js socket.io