public int HandleRequestMessage(UDPModel uModel) { KevSocketModel <ChatTextMessageModel> ksModel_ctmModel = JsonHelper.ParseFromJson <KevSocketModel <ChatTextMessageModel> >(uModel); if (ksModel_ctmModel == null || ksModel_ctmModel.Data == null) { return(ResponseCode.AnalyticalDataError); } ChatTextMessageModel ctmModel = ksModel_ctmModel.Data; ctmModel.SendUserId = ksModel_ctmModel.DeviceId; ctmModel.ReceiveUserId = ksModel_ctmModel.ReceiveDeviceId; IPEndPoint ip = UserCache.GetUserIP(ctmModel.ReceiveUserId); if (ip == null) { return(ResponseCode.NotFindUserIP); } //转发消息 KevRegister.Get <UDPServer>(UDPPrimaryKey.UDPServer).SendMessage(ksModel_ctmModel, ip); return(ResponseCode.Success); }
public void AddUserRange(IEnumerable <UserInfoModel> items) { if (items == null || items.Count() == 0) { return; } int count = 0; long id = KevRegister.Get <long>(UDPPrimaryKey.Client_ThisDeviceId, -1); foreach (UserInfoModel uiModel in items) { if (uiModel.UserId == id) { continue; } uiModels.Add(uiModel); count++; } if (count > 0) { RedrawViewByUserInfoModels(uiModels); } }
public int HandleResponseMessage(UDPModel uModel) { KevSocketModel <UserInfoModel> userInfoModel = JsonHelper.ParseFromJson <KevSocketModel <UserInfoModel> >(uModel.Body, uModel.Length); if (userInfoModel == null) { return(ResponseCode.AnalyticalDataError); } if (userInfoModel.ResponseCode != ResponseCode.Success) { KevRegister.Get <Dispatcher>(ClientItemsPrimaryKey.Dispatcher_MainThread).Invoke(() => { KevRegister.Get <HomeForm>(ClientItemsPrimaryKey.Form_Home).label_status.Text = ResponseCode.GetDescription(userInfoModel.ResponseCode); }); return(ResponseCode.NetworkHostError); } if (userInfoModel.Data == null) { return(ResponseCode.AnalyticalDataError); } KevRegister.Get <Dispatcher>(ClientItemsPrimaryKey.Dispatcher_MainThread).Invoke(() => { HomeForm homeForm = KevRegister.Get <HomeForm>(ClientItemsPrimaryKey.Form_Home); homeForm.label_nickName.Text = userInfoModel.Data.NickName; homeForm.label_signature.Text = userInfoModel.Data.Signature; }); return(ResponseCode.Success); }
public int HandleResponseMessage(UDPModel uModel) { KevSocketModel <IEnumerable <UserInfoModel> > ksModel = JsonHelper.ParseFromJson <KevSocketModel <IEnumerable <UserInfoModel> > >(uModel); if (ksModel == null) { return(ResponseCode.AnalyticalDataError); } if (ksModel.ResponseCode != ResponseCode.Success) { KevRegister.Get <Dispatcher>(ClientItemsPrimaryKey.Dispatcher_MainThread).Invoke(() => { KevRegister.Get <HomeForm>(ClientItemsPrimaryKey.Form_Home).label_status.Text = ResponseCode.GetDescription(ksModel.ResponseCode); }); return(ResponseCode.Error); } if (ksModel.Data == null) { return(ResponseCode.AnalyticalDataError); } KevRegister.Get <Dispatcher>(ClientItemsPrimaryKey.Dispatcher_MainThread).Invoke(() => { KevRegister.Get <HomeForm>(ClientItemsPrimaryKey.Form_Home).AddUserRange(ksModel.Data); foreach (UserInfoModel item in ksModel.Data) { UserCache.Updata(item); } }); return(ResponseCode.Success); }
public int HandleRequestMessage(UDPModel uModel) { KevSocketModel ksModel = JsonHelper.ParseFromJson <KevSocketModel>(uModel); if (ksModel == null) { return(ResponseCode.AnalyticalDataError); } UDPServer udpServer = KevRegister.Get <UDPServer>(UDPPrimaryKey.UDPServer); //根据当前的请求返回相应数据 KevSocketModel <UserInfoModel> ksModel_result = new KevSocketModel <UserInfoModel> { Data = UserCache.GetUserInfo(ksModel.DeviceId, () => { return(new UserInfoModel { UserId = ksModel.DeviceId, NickName = "匿名:" + ksModel.DeviceId % 1000000, Signature = "你看不见我,看不见我" }); }), ResponseCode = ResponseCode.Success, MessageId = ksModel.MessageId, MessageType = GetType(), NetworkType = NetworkType.Request }; ksModel_result.NetworkType = NetworkType.Response; ksModel_result.DeviceId = ksModel.DeviceId; udpServer.SendMessage(ksModel_result, uModel.IPPoint); return(ResponseCode.NoResponse); }
public void HandleTimeoutMessage(KevMessageBoxModel ksbModel) { if (ksbModel != null && ksbModel.SocketModel != null) { KevRegister.Get <UDPClient>(ClientItemsPrimaryKey.Socket_UDPClient).SendMessage(ksbModel.SocketModel); } ; }
void vScrollBar_box_ValueChanged(object sender, EventArgs e) { ScrollBar scrollBar = sender as ScrollBar; if (scrollBar == null) { return; } panel_box.Location = new Point(0, scrollBar.Value * -5); KevRegister.Get <HomeForm>(ClientItemsPrimaryKey.Form_Home).label_loginStatus.Text = scrollBar.Value.ToString(); }
public void HandleTimeoutMessage(KevMessageBoxModel ksbModel) { Dispatcher dispatcher = KevRegister.Get <Dispatcher>(ClientItemsPrimaryKey.Dispatcher_MainThread); dispatcher.Invoke(() => { MainForm mainForm = KevRegister.Get <MainForm>(ClientItemsPrimaryKey.Form_Main); if (mainForm != null) { mainForm.ShowAPrompt("连接服务器超时了"); } }); }
public int HandleRequestMessage(UDPModel uModel) { KevSocketModel ksModel = JsonHelper.ParseFromJson <KevSocketModel>(uModel); if (ksModel == null) { return(ResponseCode.AnalyticalDataError); } IEnumerable <long> userIds = UserCache.GetAllUserId(); if (userIds == null || userIds.Count() == 0) { return(ResponseCode.NotFind); } List <UserInfoModel> uiModels = new List <UserInfoModel>(); foreach (long id in userIds) { UserInfoModel uiModel = UserCache.GetUserInfo(id, () => { return(new UserInfoModel { UserId = id, NickName = "匿名:" + id % 10000, Signature = "你看不见我,看不见我" }); }); if (uiModel != null) { uiModels.Add(uiModel); } } KevSocketModel <IEnumerable <UserInfoModel> > ksModel_userInfoModel = new KevSocketModel <IEnumerable <UserInfoModel> > { Data = uiModels, DeviceId = ksModel.DeviceId, MessageId = ksModel.MessageId, MessageType = GetType(), NetworkType = NetworkType.Response, ResponseCode = ResponseCode.Success }; KevRegister.Get <UDPServer>(UDPPrimaryKey.UDPServer).SendMessage(ksModel_userInfoModel, uModel.IPPoint); return(ResponseCode.NoResponse); }
public void AddUser(UserInfoModel uiModel, bool isRedrawView = true) { long id = KevRegister.Get <long>(UDPPrimaryKey.Client_ThisDeviceId, -1); if (uiModel.UserId == id) { return; } uiModels.Add(uiModel); if (isRedrawView) { RedrawViewByUserInfoModels(uiModels); } }
public int HandleRequestMessage(UDPModel uModel) { //哎呀有人上线了,怎么办呢。 KevSocketModel <UserInfoModel> ksModel_userInfo = JsonHelper.ParseFromJson <KevSocketModel <UserInfoModel> >(uModel); if (ksModel_userInfo == null || ksModel_userInfo.Data == null) { return(ResponseCode.AnalyticalDataError); } UserCache.Updata(ksModel_userInfo.Data); KevRegister.Get <Dispatcher>(ClientItemsPrimaryKey.Dispatcher_MainThread).Invoke(() => { KevRegister.Get <HomeForm>(ClientItemsPrimaryKey.Form_Home).AddUser(ksModel_userInfo.Data); }); return(ResponseCode.Success); }
public int HandleResponseMessage(UDPModel uModel) { KevSocketModel ksModel = JsonHelper.ParseFromJson <KevSocketModel>(uModel); if (ksModel == null) { return(ResponseCode.AnalyticalDataError); } if (ksModel.ResponseCode != ResponseCode.Success) { KevRegister.Get <Dispatcher>(ClientItemsPrimaryKey.Dispatcher_MainThread).Invoke(() => { KevRegister.Get <HomeForm>(ClientItemsPrimaryKey.Form_Home).label_status.Text = ResponseCode.GetDescription(ksModel.ResponseCode); }); } return(ResponseCode.Success); }
public int HandleRequestMessage(UDPModel uModel) { KevSocketModel <ChatTextMessageModel> ksModel_ctmModel = JsonHelper.ParseFromJson <KevSocketModel <ChatTextMessageModel> >(uModel); if (ksModel_ctmModel == null || ksModel_ctmModel.Data == null) { return(ResponseCode.AnalyticalDataError); } //界面上展示消息 KevRegister.Get <Dispatcher>(ClientItemsPrimaryKey.Dispatcher_MainThread).Invoke(() => { HomeForm hForm = KevRegister.Get <HomeForm>(ClientItemsPrimaryKey.Form_Home); ChatMessageCache.HandleMessage(ksModel_ctmModel.Data); hForm.HandleMessage(ksModel_ctmModel.Data); }); return(ResponseCode.Success); }
private void button_login_Click(object sender, EventArgs e) { UDPClient udpClient = KevRegister.Get <UDPClient>(ClientItemsPrimaryKey.Socket_UDPClient, () => { udpClient = new UDPClient(); udpClient.ServerIPPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7788); udpClient.Start(); return(udpClient); }); KevSocketModel ksModel = new KevSocketModel() { DeviceId = KevRegister.Get(UDPPrimaryKey.Client_ThisDeviceId, -1), MessageId = IdGenerator.NextId(), MessageType = MessageType.Login, NetworkType = NetworkType.Request }; if (!udpClient.SendMessage(ksModel)) { MessageBox.Show("连接服务器失败,请稍后尝试"); } }
private void button_chatBox_sendMessage_Click(object sender, EventArgs e) { long selfUserId = KevRegister.Get <long>(UDPPrimaryKey.Client_ThisDeviceId, -1); KevSocketModel <ChatTextMessageModel> ksModel_ctmModel = new KevSocketModel <ChatTextMessageModel>() { Data = new ChatTextMessageModel { Message = this.textBox_chatBox_userInput.Text }, DeviceId = selfUserId, MessageId = IdGenerator.NextId(), MessageType = MessageType.ChatTextMessage, NetworkType = NetworkType.Request, ReceiveDeviceId = this.ChatUserId }; AddMessageToView("我", this.textBox_chatBox_userInput.Text); if (KevRegister.Get <UDPClient>(UDPPrimaryKey.UDPClient).SendMessage(ksModel_ctmModel)) { this.textBox_chatBox_userInput.Text = string.Empty; } }
public int HandleResponseMessage(UDPModel uModel) { //判断数据 KevSocketModel <long> ksModel = JsonHelper.ParseFromJson <KevSocketModel <long> >(uModel.Body, uModel.Length); if (ksModel == null) { return(ResponseCode.NotFindKevSocketModel); } //处理相关的逻辑然后进行跳转 Dispatcher dispatcher = KevRegister.Get <Dispatcher>(ClientItemsPrimaryKey.Dispatcher_MainThread); MainForm mainForm = KevRegister.Get <MainForm>(ClientItemsPrimaryKey.Form_Main); if (ksModel.ResponseCode == ResponseCode.Success) { dispatcher.Invoke(() => { if (mainForm != null) { mainForm.Hide(); } //替换本机的Id KevRegister.Add <long>(UDPPrimaryKey.Client_ThisDeviceId, ksModel.Data); KevRegister.Get <HomeForm>(ClientItemsPrimaryKey.Form_Home, () => { return(new HomeForm()); }).Show(); }); //请求本机资料 KevSocketModel ksModel_requestSelfUserInfo = new KevSocketModel { DeviceId = KevRegister.Get <long>(UDPPrimaryKey.Client_ThisDeviceId, -1), MessageId = IdGenerator.NextId(), MessageType = MessageType.GetUserInfo, NetworkType = NetworkType.Request }; UDPClient udpClient = KevRegister.Get <UDPClient>(ClientItemsPrimaryKey.Socket_UDPClient); if (!udpClient.SendMessage(ksModel_requestSelfUserInfo)) { KevRegister.Get <HomeForm>(ClientItemsPrimaryKey.Form_Home).label_status.Text = "请求自己的资料失败"; } //获取已经登录的人 KevSocketModel ksModel_getFriendList = new KevSocketModel { DeviceId = KevRegister.Get <long>(UDPPrimaryKey.Client_ThisDeviceId, -1), MessageId = IdGenerator.NextId(), MessageType = MessageType.GetUserFriendList, NetworkType = NetworkType.Request }; udpClient.SendMessage(ksModel_getFriendList); } else { dispatcher.Invoke(() => { mainForm.ShowAPrompt(ResponseCode.GetDescription(ksModel.ResponseCode)); }); } return(ResponseCode.NoResponse); }
/// <summary> /// 处理消息 /// </summary> /// <param name="uModel"></param> /// <returns></returns> public int HandleRequestMessage(UDPModel uModel) { KevSocketModel <LoginSocketModel> loginModel = JsonHelper.ParseFromJson <KevSocketModel <LoginSocketModel> >(uModel.Body, uModel.Length); if (loginModel == null) { return(ResponseCode.NotFindKevSocketModel); } if (loginModel.NetworkType == NetworkType.Response) { return(ResponseCode.NoResponse); } UDPServer udpServer = KevRegister.Get <UDPServer>(UDPPrimaryKey.UDPServer); if (udpServer == null) { return(ResponseCode.NotFindUDPServer); } //判断是否正常登陆 long newLoginId = IdGenerator.NextId(); KevSocketModel <UserInfoModel> ksModel_newUserLogin = new KevSocketModel <UserInfoModel> { Data = UserCache.GetUserInfo(newLoginId, () => new UserInfoModel { UserId = newLoginId, NickName = "匿名:" + newLoginId % 1000000, Signature = "你看不见我,看不见我" }), DeviceId = newLoginId, MessageId = IdGenerator.NextId(), NetworkType = NetworkType.Request, MessageType = MessageType.NewUserLogin }; //广播给之前的所有用户 foreach (long uId in UserCache.GetAllUserId()) { ksModel_newUserLogin.ReceiveDeviceId = uId; IPEndPoint ip = UserCache.GetUserIP(uId); if (ip != null) { udpServer.SendMessage(ksModel_newUserLogin, ip); } } //重写自己的回执 KevSocketModel <long> ksModel = new KevSocketModel <long> { ResponseCode = ResponseCode.Success, MessageId = loginModel.MessageId, MessageType = MessageType.Login, NetworkType = NetworkType.Response, ReceiveDeviceId = -1, Data = newLoginId }; udpServer.SendMessage(ksModel, uModel.IPPoint); UserCache.AddUser(newLoginId, uModel.IPPoint); Console.WriteLine("有人成功登陆"); return(ResponseCode.NoResponse); }