简体   繁体   English

NodeJS socket.io套接字不发出

[英]NodeJS socket.io socket does not emit

I'm working using socket.io on NodeJS to create bots for MooMoo.io. 我正在使用NodeJS上的socket.io为MooMoo.io创建机器人。 Everything seems to work fine except the bots will not accept a clan invitation. 一切似乎都正常,除非机器人不接受氏族邀请。

Here is the part that is broken: 这是损坏的部分:

socket.emit("11", a, 1); // a = player ID

Here's my code: 这是我的代码:

const io = require("socket.io-client");
var socket = io.connect("http://52.39.43.139:" + (5006) + '', {
    reconnection: false,
    query: "man=1"
});
var n = 0;

function spawn(i) {
    n++;
    if (n >= 10) return null;
    socket = io.connect("http://52.39.43.139:" + (5000 + i % 11) + '', {
        reconnection: false,
        query: "man=1"
    });
    socket.once("connect", () => {
        socket.emit("1", {name: "ME"}); // spawn
        socket.emit("10", "ME"); // request to join clan
    });
}
socket.once("connect", () => {
    socket.emit("1", {
        name: "ME"
    }); // spawn
    socket.emit("8", "ME"); // create clan
    console.log("Clan created");
});

team = [];

var i = 0;
setInterval(() => spawn(i), 3000);
socket.on("an", (a, name) => { // player requests to join clan event
    socket.emit("11", a, 1); // accept invite
    console.log(`${name} successfully joined :)`);
    team.push(a);
    console.log(team);
});
socket.on("ad", (a) => { // player leaves clan event
    for (var c = team.length - 1; c >= 0; c--)
        if (team[c].sid == a) {
            team.splice(c, 1);
            console.log(team[c].name + ' has left');
        }
    console.log(team);
});
socket.on("11", () => { // respawn event
    console.log("I have died, respawning...")
    socket.emit("1", { // spawn event
        name: "ME"
    });
});

According to the part you described as being broke you shouldn't pass extra variables like this: 根据您描述为损坏的部分,您不应传递这样的额外变量:

socket.emit("11", a, 1); // a = player ID

But rather it should look like this; 而是应该看起来像这样;

socket.emit("11", {
   playerID:a,
   otherVariable:1
}); 

Which you can acess in your server code by using: 您可以使用以下命令在服务器代码中访问哪个代码:

socket.on('11', function(data){
   console.log(data.playerID);
   console.log(data.otherVariable);
});

If you can provide more detail about other problems your having I can try to help you further, but you only highlighted that that emit didn't work and we don't have your backend code to reference here. 如果您可以提供有关其他问题的更多详细信息,请尝试为您提供进一步的帮助,但是您仅强调指出,该问题不起作用,我们这里没有您的后端代码可供参考。

Another note, You shouldn't be passing anything but either a single variable (which the documentation still doesn't link and prefers passing via object) or an object. 另一个要注意的是,除了一个变量(文档仍然不链接并且更喜欢通过object传递)或一个对象之外,您不应传递任何东西。 You also shouldn't be using socket.once in this context as it's not needed in my opinion 在这种情况下,您也不应该使用socket.once ,因为我认为不需要

Turns out the server limited one connection per port. 原来服务器每个端口限制一个连接。 Fixed by using different ports. 通过使用不同的端口进行修复。

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

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