public void Init(EndPoint endpoint, int MaxReceiveLength) { m_OPTypeLength = 1; m_OPCodeLength = 1; m_ParaCountLength = 1; m_PerParaCountLength = 2; UserCommandBase[] baseArray = new UserCommandBase[] { new DoLoginResult(), new DoUserJoin(), new DoGetAllUser(), new DoGetUser(), new DoWhisper(), new DoBroadCast(), new DoUserLeave(), new DoUserStartCreateChat(), }; foreach (UserCommandBase base2 in baseArray) { this.m_CommandDict.Add(base2.Name, base2); } MyAsyncTcpSession session = new MyAsyncTcpSession(endpoint, MaxReceiveLength); session.Error += session_Error; session.DataReceived += session_DataReceived; session.Connected += session_Connected; session.Closed += session_Closed; session.GetSocket += session_GetSocket; this.Client = session; this.CommandReader = new UserReader(this); }
public void ChatConn(string toUserAddr, int toUserPort) { ToUserAddr = toUserAddr; ToUserPort = toUserPort; EndPoint endpoint = new IPEndPoint(IPAddress.Parse(toUserAddr), toUserPort - 1); MyAsyncTcpSession chatsession = new MyAsyncTcpSession(endpoint, 1024 * 10); chatsession.Error += chatsession_Error; chatsession.DataReceived += chatsession_DataReceived; chatsession.Connected += chatsession_Connected; chatsession.Closed += chatsession_Closed; Client = chatsession; Client.Connect(); if (!m_OpenedEvent.WaitOne(5000)) { throw new Exception("连接超时"); } var currentSessionIdData = Encoding.UTF8.GetBytes(CurrentUser.SessionId); this.SendData(UserOP.DoNewChat, currentSessionIdData); }