使用socket.io和node.js从服务器向客户端发送消息
Send message to client from server using socket.io and node.js
我使用的是socket.io和node.js。我可以从服务器向所有客户端广播消息,但在从服务器向特定客户端发送消息时遇到了问题。我是这个socket.io和node.js 的新手
以下是为客户和服务的代码剪辑
服务器代码:
var http = require('http'),
fs = require('fs');
var express = require('express');
var app = http.createServer(function (request, response) {
fs.readFile("client.html", 'utf-8', function (error, data) {
if(error)
{
responce.writeHead(404);
responce.write("File does not exist");
responce.end();
}
else
{
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
}
});
}).listen(1337);
var io = require('socket.io').listen(app);
var clients = [ ] ;
var socketsOfClients = {};
io.sockets.on('connection', function(socket)
{
socket.on('message_to_server', function(data)
{
clients.push(socket.id);
socket(clients[0]).emit("message_to_client" , { message: data["message"] });
});
});
~
客户端代码:
<!DOCTYPE html>
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
// our socket.io code goes here
var socketio = io.connect("127.0.0.1:1337");
socketio.on("message_to_client", function(data) {
document.getElementById("chatlog").innerHTML = ("<hr/>" +
data['message'] + document.getElementById("chatlog").innerHTML);
});
function sendMessage() {
var msg = document.getElementById("message_input").value;
socketio.emit("message_to_server", { message : msg});
}
</script>
</head>
<body>
<input type="text" id="message_input"/>
<button onclick="sendMessage()">send</button>
<div id="chatlog"></div>
</body>
</html>
~
当我执行时,它会给出错误,比如:
socket(clients[0]).emit("message_to_client" , { message: data["message"] });
^
TypeError:对象不是函数在Socket。
我想我明白你想做什么了。你想通过使用消息的ID将消息寻址到特定的套接字。
根据文档,socket
不是函数(http://socket.io/docs/server-api/#socket),所以像socket()
一样调用它会导致代码失败。
与其将套接字存储在数组中,不如尝试将它们存储在哈希中:
var clients = {};
io.sockets.on('connection', function (socket) {
// store a reference to this socket in the hash, using
// its id as the hash key
clients[socket.id] = socket;
socket.on('message_to_server', function (data) {
// look up a client socket by id (this would come from
// the client, and would need to be communicated to the
// client in some way, perhaps with a broadcast message
// sent to every client whenever another client "logged in"
var destination = clients[data.destinationId];
// if destination is undefined (falsy) it does not
// exist in the hash
if (!destination) {
return;
}
// send a message to the destination
destination.emit("message_to_client" , { message: data["message"] });
});
});
如果您only想要将消息发送到连接期间创建的同一套接字,则不必将对它的引用存储在哈希或数组中,因为您可以在闭包中访问它:
io.sockets.on('connection', function (socket) {
socket.on('message_to_server', function (data) {
// we have a reference to socket from the closure above ^
socket.emit("message_to_client" , { message: data["message"] });
});
});
相关文章:
- Node.js上的WebSocket,并在所有连接的客户端之间共享消息
- 为什么我的WebSocket客户端总是在发送消息后重新加载
- 无法从客户端发出套接字消息
- 如何将错误消息从 meteor 服务器传递到客户端
- Socket.IO 客户端库提供“欢迎来到 socket.io”消息
- 服务器无法从客户端获取消息
- 客户端验证 没有验证的属性上的成功消息
- Nodejs-websocket:如何在没有服务器响应的情况下连续发送客户端消息
- C#/ASP-web,ASP:在客户端执行复选框消息框,如果是,则运行服务器端代码
- 在服务器上压缩JSON消息,在客户端解压缩
- 在socket.io中从客户端发送广播消息
- 插入之前的客户端是/否确认消息
- 如何确保客户端已收到通过socket.io发送的消息
- socket.io 客户端,不会向服务器发出消息
- sails 客户端(浏览器)如何侦听从不同端口发出(爆破)的套接字消息
- 有没有办法在事件发生时将消息从服务器发送到客户端
- 无法使用 Firefox 中的信号器向客户端发送消息
- Nodejs,客户端并不总是接收服务器消息
- 棘轮客户端消息
- Asp.net MVC-从服务器推送延迟的客户端消息