JS Socket.io:客户端未捕获事件

huangapple go评论75阅读模式
英文:

JS Socket.io: Client not catching event

问题

我正在尝试在我的应用程序中集成socket.io,我将使用它来传递命令和实时更新从服务器到客户端。我能够连接客户端和服务器,也能够将客户端放入一个房间中,我将在其中发出我的事件。但是,客户端似乎无法捕获事件。

客户端:

在窗口加载时连接客户端:

const socket = io("/");

socketConnect = function(){
    socket.on("connect", () => {
    	console.log(socket.id);
    })
};

window.onload = socketConnect();

客户端点击上传按钮并提交表单,然后等待服务器的确认:

(注意:这是与上述代码不同的.js文件。)

submitForms = function(){

    socket.on("formOK", () => {
        console.log("form was sent");
        socket.disconnect();
    })

    document.getElementById("form1").submit();
}

服务器端:

获取连接并将客户端放入一个房间:

// 初始化 socket.io
io.on("connection", (socket) =>{
	clientID = socket.id;
	roomID = `ROOM:${clientID}`;

	// 连接客户端到将交换消息的房间(房间名 = "ROOM:clientId")
	socket.join(roomID);
});

服务器处理表单并向客户端确认成功:

app.post("/uploads/kit-info", (req,res) => {
	//... 处理 post 请求

	// 向客户端发送成功确认
	io.to(roomID).emit("formOK");
});

问题:

一切正常,但我无法在客户端收到"formOK"确认。我运行了测试并确认客户端确实在房间中,所以io.to(roomID).emit("formOK"); 应该正常工作,对吗?

我不确定出了什么问题。

英文:

I'm trying to integrate socket.io in my app, I will use it as a way to pass commands and real-time updates from server to client. I am able to connect both client and server and I'm also able to put the client in a room in which I will be emiting my events. However, the client doesn't seem to be catching events.

CLIENT-SIDE:

Connects client when window loads:

const socket = io("/");

socketConnect = function(){
    socket.on("connect", () => {
    	console.log(socket.id);
    })
};

window.onload = socketConnect();

Client clicks an upload button and form gets submitted, then client will wait for a confirmation from server:

(NOTE: This is a different .js file from the above code.)

submitForms = function(){

    socket.on("formOK", () => {
        console.log("form was sent");
        socket.disconnect();
    })

    document.getElementById("form1").submit();
}

SERVER-SIDE:

Gets connection and puts client in a room:

//initiates socket.io
io.on("connection", (socket) =>{
	clientID = socket.id;
	roomID = `ROOM:${clientID}`;

	//connects client to room where messages will be exchanged (room name = "ROOM:clientId")
	socket.join(roomID);
});

Server handles form and confirms success to client:

app.post("/uploads/kit-info", (req,res) => {
	//... handles post request

	//sends success confirmation to client
	io.to(roomID).emit("formOK");
	});

The problem:

Everything works fine but I am unable to get the "formOK" confirmation on the client-side. I've run tests and I confirm that the client is indeed in the room, so io.to(roomID).emit("formOK"); should be working, right?

I am not sure what is going wrong.

答案1

得分: 1

你立即调用了socketConnect函数,而不是将它分配为回调函数。

window.onload = socketConnect()
window.onload = socketConnect //将其更改为这样。

在你的submitForms函数中,你在函数内部注册了"formOK"事件处理程序。这意味着每次调用submitForms时,都会注册一个新的事件处理程序。因此,在第一次提交后,将会有多个事件处理程序监听"formOK"事件。你可以将事件处理程序的注册移到submitForms函数之外,并在客户端连接到socket时调用它。更新你的客户端代码如下:

const socket = io("/")

socket.on("connect", () => {
  console.log(socket.id)

  socket.on("formOK", () => {
    console.log("表单已发送")
    socket.disconnect()
  })
})

window.onload = socketConnect
英文:

you are immediately invoking the socketConnect function instead of assigning it as a callback.

window.onload = socketConnect() 
window.onload = socketConnect //change it to this. 

in your submitForms function, you're registering the event handler for the "formOK" event inside the function. This means that every time submitForms is called, a new event handler is registered. As a result after the first submission, multiple event handlers will be listening for the "formOK" event. you can move the event handler registration outside the submitForms function and call it once when the client connects to the socket. update your client side code to something like this,

const socket = io("/")

socket.on("connect", () => {
  console.log(socket.id)

  socket.on("formOK", () => {
    console.log("form was sent")
    socket.disconnect()
  })
})

window.onload = socketConnect

huangapple
  • 本文由 发表于 2023年7月14日 08:24:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76684009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定