Javascript 在 node.js 上的 socket.io 上发送消息 client->server->client

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10600496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:05:01  来源:igfitidea点击:

Sending messages client->server->client on socket.io on node.js

javascriptjquerysocketsnode.js

提问by test

I'm using NodeJS+Socket.IO for simple web game. It works. Why?

我正在使用 NodeJS+Socket.IO 进行简单的网页游戏。有用。为什么?

Here's my server.js

这是我的 server.js

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

io.sockets.on('connection', function (socket) {
socket.on('set nickname' , function (nickname) {
socket.nickname = nickname;
console.log(nickname + ' just connected!');

});

socket.on('msg' , function (msg) {
socket.msg = msg;
io.sockets.emit('response', msg);
});

socket.on('updatePlayer' , function (updatePlayer) {
console.log("Someone just moved on the map!");
});

});

app.listen(8080);

The reason I know it works is because, when I load the page and this script executes:

我知道它有效的原因是,当我加载页面并执行此脚本时:

// SERVER STUFF
socket.on('connect', function () {
    console.log('We are connected!');
    name = $(".name").val();
    this.emit('set nickname', name, function (success) {});
    msg = name + "|" + currX + "|" + currY + "|" + currentMap;
    this.emit('msg', msg, function (success) {});

    socket.on('response', function (data) {
        var theMsg = data.split("|");
        sayDialog("New player! " + theMsg[0] + " connected on map " + theMsg[3] + " on coordinates (" + theMsg[1] + "," + theMsg[2] + ")");
    });
});

I get in my dialogbox ("CSS div") something like New player! weka connected on map 4 on coordinates (10,5)

我进入我的对话框(“CSS div”),类似于 New player! weka connected on map 4 on coordinates (10,5)

OK, cool, so it works! However, when I "move" I try sending a message to server using this

好的,很酷,所以它起作用了!但是,当我“移动”时,我尝试使用它向服务器发送消息

  socket.on('updatePlayer', function () {
    console.log("Testing...");
  }); 

I don't even see consolesay Testing. I don't know why.

我什至没有看到console说测试。我不知道为什么。

回答by Wilk

Client-side.A player moves and the function movePlayer is executed:

客户端。玩家移动并执行 movePlayer 函数:

function movePlayer () {
    socket.emit ('player move', {map: 4, coords: '0.0'});
}

socket.on ('updatePlayer', function (msg) {
    console.log ('A player moves on map ' + msg.map + ' on coords ' + msg.coords);
});

Server-side.When someone moves, a 'updatePlayer' is emitted for each socket except the original player. On each client side, the updatePlayer is intercepted.

服务器端。当有人移动时,除了原始玩家之外,每个套接字都会发出一个“updatePlayer”。在每个客户端,都会拦截 updatePlayer。

socket.on ('player move', function (msg) {
    io.sockets.emit ('updatePlayer', msg);
});

回答by Tronix117

Server side

服务器端

socket.on('updatePlayer', function(updatePlayer){
  console.log("Someone just moved on the map!")
  updatePlayer() // will trigger the client side function
});

Client side

客户端

socket.emit('updatePlayer', function(){
  console.log('testing');
})