Send additional data on socket connection

前端 未结 7 521
清酒与你
清酒与你 2020-12-24 10:12

How to best send additional data upon socket connection?

Client:

socket.on(\'connect\',function(){ 
//I\'d like set some values and pass them up to t         


        
7条回答
  •  没有蜡笔的小新
    2020-12-24 10:37

    The connection events get fired as soon as the TCP connection is established. There's no way to send anything in between.

    What you can do is to simply take the first message send by the server and put the data in that message. I'd strongly suggest to roll some thin protocol for this, so you would have multiple types of messages and use those to determine how the code should process the data.

    This would be more extendable too, since it's fairly easy to come up with a generic architecture for that. If you want to wait for the userdata to come in first you can simply add some state to your connections.

提交回复
热议问题