简体   繁体   English

socket.io向每个客户端发出/广播

[英]socket.io emit/broadcast to every client

I can not find out how to emit from the client or an other NodeJS file to every client. 我不知道如何从客户端或其他NodeJS文件向每个客户端发出信号。 Emitting to the server worked, but not to every other client. 发送到服务器有效,但不发送给其他所有客户端。

Servercode: Servercode:

var io = require('socket.io').listen(app.listen(8080));

io.on('connection', function (socket) {
    socket.on('playerData', function(data) {
        console.log(data);
    });
});

Clientcode: Clientcode:

<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script> 
<script>
var socket = io('http://localhost:8080');
socket.on('playerData', function(data) {
    console.log(data);
});
</script>

I am using Node.JS - Jade, Express, Socket.io (and AngularJS, but not in this part of the code) 我正在使用Node.JS-Jade,Express,Socket.io(和AngularJS,但不在此部分代码中)

The Serverside console.log is being called when I call my emit function, but on the Clientside nothing happens. 当我调用我的emit函数时,服务器端console.log被调用,但是在客户端却什么也没有发生。 Currently using Chrome as my browser. 当前使用Chrome作为我的浏览器。

您需要从客户端发送到服务器,然后将该消息从服务器广播到每个客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM