void tcpServerEngine_NewSessionConnected(MyAppSession session) { this.ShowMessage(session.RemoteEndPoint, "上线"); mOnLineConnections.TryAdd(session.SessionID, session); this.ShowConnectionCount(mOnLineConnections.Count); //this.ShowConnectionCount(this.tcpServerEngine.SessionCount); }
private void button_Send_Click(object sender, EventArgs e) { try { MyAppSession appSession = (MyAppSession)this.cboClients.SelectedItem; if (appSession == null) { ShowMessage("没有选中任何在线客户端!"); return; } if (!appSession.Connected) { ShowMessage("目标客户端不在线!"); return; } string msg = string.Format("push {0}", this.textBox_msg.Text + Environment.NewLine); //一定要加上分隔符 byte[] bMsg = System.Text.Encoding.UTF8.GetBytes(msg); //消息使用UTF-8编码 //this.tcpServerEngine.GetSessionByID(appSession.SessionID).Send(new ArraySegment<byte>(bMsg, 0, bMsg.Length)); appSession.Send(new ArraySegment <byte>(bMsg, 0, bMsg.Length)); } catch (Exception ee) { ShowMessage(ee.Message); } }
void tcpServerEngine_SessionClosed(MyAppSession session, global::SuperSocket.SocketBase.CloseReason value) { this.ShowMessage(session.RemoteEndPoint, "下线"); MyAppSession outAppSession; mOnLineConnections.TryRemove(session.SessionID, out outAppSession); this.ShowConnectionCount(mOnLineConnections.Count); //this.ShowConnectionCount(this.tcpServerEngine.SessionCount); }
void tcpServerEngine_NewRequestReceived(MyAppSession session, global::SuperSocket.SocketBase.Protocol.StringRequestInfo requestInfo) { switch (requestInfo.Key) { case "echo": this.ShowMessage(session.RemoteEndPoint, requestInfo.Body); break; case "heartbeat": this.ShowMessage(session.RemoteEndPoint, requestInfo.Body); string msg = string.Format("push {0}", requestInfo.Body + Environment.NewLine); //一定要加上分隔符 byte[] bMsg = System.Text.Encoding.UTF8.GetBytes(msg); //消息使用UTF-8编码 session.Send(new ArraySegment <byte>(bMsg, 0, bMsg.Length)); break; default: this.ShowMessage(session.RemoteEndPoint, "未知的指令(error unknow command)"); break; } }