kerryjiang / SuperWebSocket

SuperWebSocket is a .NET implementation of WebSocket server.
Apache License 2.0
505 stars 194 forks source link

session.Connected sometimes false #36

Closed BinLiang1990 closed 7 years ago

BinLiang1990 commented 7 years ago

问题 后端foreach(var session in tvSession.AppServer.GetAllSessions())的session.Connected有时候是false,此时session.send(xxx)发送消息失败

本地环境

后端 使用了自定义WebSocketSession 在类 xxx : SubCommandBase中 foreach(var session in tvSession.AppServer.GetAllSessions())

前端 在ws.onopen的时候ws.send,获取相应数据,此时的ws.readyState=1,理论上应该是没问题的

尝试 尝试过在onopen之后延迟send 以及在onopen之外通过setTimeout+判断readyState==1之后再send的方式 发现使用xx:SubCommandBase的方式会出现这个问题 后端直接使用ws.NewMessageReceived的方式,前端延迟send就可以解决问题,但是太不优雅,如你所说,不符合OOD原则

BinLiang1990 commented 7 years ago

question when i received new message , and i want to send message back to client. the session.Connected sometimes false and session.send(xxx) failed

Local environment

back-end i use CustomSession to save the property (like roomid) use Command way like

 public class GetMessage : SubCommandBase<XxxxSession>
    {
        public override void ExecuteCommand(XxxxSession xSession, SubRequestInfo requestInfo)
        {
               //dosth to get message
               ....
               foreach(var session in tvSession.AppServer.GetAllSessions())
               {
                      //some times session.Connected = false
                      session.send(xxxx)
               }
       }
}

fore-end ws.onopen to send message. ws.readyState=1 this moment

try i tried many ways like onopen+setTimeout
or setTimeout+readyState==1 outside of onopen function

Is there some suggestions?

BinLiang1990 commented 7 years ago

通过tvSession.AppServer.GetAllSessions是不能给自己发,但是可以xSession直接给自己发,这样也能搞定