/// <summary> /// 给聊天室里的某人单独发消息 /// </summary> /// <param name="session"></param> /// <param name="reader"></param> static void OnSendPrivateMessage(Session session, PacketReader reader) { var userName = reader.ReadUTF8String(); var message = reader.ReadUTF8String(); if (message == null) return; var target = sessions.FirstOrDefault(o => o.Name == userName); if (target == null) return; var writer = new PacketWriter(); writer.SetNetCode((ushort) OpCode.RecvPrivateMessage); writer.WriteUTF8Null(session.Name); writer.WriteUTF8Null(message); target.Client.SendPackage(writer.GetBuffer()); }
/// <summary> /// socket连接 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void OnSocketConnect(object sender, SocketConnectEventArgs<Session> e) { // 互相绑定 var s = new Session(); s.Client = e.Session; e.Session.Data = s; nologinSessions.Add(s); }
/// <summary> /// 给聊天室里的人都发消息 /// </summary> /// <param name="session"></param> /// <param name="reader"></param> static void OnSendMessage(Session session, PacketReader reader) { var message = reader.ReadUTF8String(); if (string.IsNullOrEmpty(message)) { // 空消息 return; } // 广播给所有在线的用户 var writer = new PacketWriter(); writer.SetNetCode((ushort)OpCode.RecvMessage); foreach (var ss in sessions) { ss.Client.SendPackage(writer.GetBuffer()); } }
/// <summary> /// 登录服务器 /// </summary> /// <param name="session"></param> /// <param name="reader"></param> static void OnLogin(Session session, PacketReader reader) { var userName = reader.ReadUTF8String(); var pwd = reader.ReadUTF8String(); if (string.IsNullOrEmpty(userName)) { Logs.Error("连接的用户名是空"); session.Client.CloseSocket(); } if (pwd != "123") { Logs.Error("用户名 {0} 速度的密码错误", userName); var writer = new PacketWriter(); writer.SetNetCode((ushort)OpCode.LoginResult); writer.Write(1); // 0表示登录成功 1表示密码错误 session.Client.SendPackage(writer.GetBuffer()); return; } // 如果玩家之前登录过,则把之前的客户端踢下线 var exists = sessions.FirstOrDefault(o => o.Name == userName); if (exists != null) { exists.IsLogin = false; sessions.Remove(exists); exists.Client.CloseSocket(); } // 登录完成 session.IsLogin = true; nologinSessions.Remove(session); sessions.Add(session); session.Name = userName; session.Pwd = pwd; session.BizId = clientIdSeq++; var writer2 = new PacketWriter(); writer2.SetNetCode((ushort)OpCode.LoginResult); writer2.Write(0); // 0表示登录成功 1表示密码错误 session.Client.SendPackage(writer2.GetBuffer()); }
/// <summary> /// 给聊天室里的某人单独发消息 /// </summary> /// <param name="session"></param> /// <param name="reader"></param> static void OnSendPrivateMessage(Session session, PacketReader reader) { var userName = reader.ReadUTF8String(); var message = reader.ReadUTF8String(); if (message == null) return; var target = sessions.FirstOrDefault(o => o.Name == userName); if (target == null) return; var buffer = MessagePackageManager.WriteSendPrivateMessageResult(session.Name, message); target.Client.SendPackage(buffer); }
/// <summary> /// 给聊天室里的人都发消息 /// </summary> /// <param name="session"></param> /// <param name="reader"></param> static void OnSendMessage(Session session, PacketReader reader) { var message = reader.ReadUTF8String(); if (string.IsNullOrEmpty(message)) { // 空消息 return; } // 广播给所有在线的用户 var buffer = MessagePackageManager.WriteSendMessageResult(message); foreach (var ss in sessions) { ss.Client.SendPackage(buffer); } }
/// <summary> /// 登录服务器 /// </summary> /// <param name="session"></param> /// <param name="reader"></param> static void OnLogin(Session session, PacketReader reader) { var packet = MessagePackageManager.ReadLogin(reader); if (string.IsNullOrEmpty(packet.UserName)) { Logs.Error("连接的用户名是空"); session.Client.CloseSocket(); } if (packet.Pwd != "123") { Logs.Error("用户名 {0} 速度的密码错误", packet.UserName); var buffer = MessagePackageManager.WriteLoginResult(LoginResult.PwdError); session.Client.SendPackage(buffer); return; } // 如果玩家之前登录过,则把之前的客户端踢下线 var exists = sessions.FirstOrDefault(o => o.Name == packet.UserName); if (exists != null) { exists.IsLogin = false; sessions.Remove(exists); exists.Client.CloseSocket(); } // 登录完成 session.IsLogin = true; nologinSessions.Remove(session); sessions.Add(session); session.Name = packet.UserName; session.Pwd = packet.Pwd; session.BizId = clientIdSeq++; var buffer2 = MessagePackageManager.WriteLoginResult(LoginResult.Success); session.Client.SendPackage(buffer2); }