先作为主站备份 源站:https://my.toho.red

Still, there are many experts on v2ex. I'll record a time when I encountered a bug and was saved by a big shot.

I have been studying a project recently (expected to be launched soon), and because there is a situation where the client needs to receive real-time information, I started to learn about websockets. However, during the development process, I encountered a quite ridiculous problem: the server could not send messages to the client no matter what. I was using the socketio library, and I initially thought it was just a common bug, but I found that I couldn't solve it even after trying various methods for several days.

The specific situation is as follows:


The original plan was that after the client successfully registered, the server would return a token to the client and bind the corresponding sid for this token. When there is a new message, the server will search for the sid based on the token and push the message.

Sounds easy, right? But the problem lies here: no matter how I record the sid, I cannot push the message correctly, and the client has no response. After trying various solutions, I had to give up torturing myself and throw the problem to V2EX, hoping that some experts could help solve it not because I'm lazy.

However, today, hope appeared: a guru directly provided the solution code. Finally, the problem was solved, and I started to study the train of thought. With a quick glance, I found a huge mistake I made: the sid I obtained from the server belongs to the server, but I must first obtain the sid from the client and pass it to the server in order for the server to locate the client and send messages.

Although it was a rookie mistake, the problem that troubled me for several days was finally solved, and I can happily write bugs again. I can only say that V friends are amazing, but it seems that I still need to improve my coding skills, otherwise, next time I may not be so lucky to encounter someone willing to help.


Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.