public override void fn_parse(string _jsonmsg) { //base.fn_parse(_jsonmsg); if (_jsonmsg == "") { return; } A_baseMsg t_basemsg = JsonMapper.ToObject<A_baseMsg>(_jsonmsg); if (t_basemsg != null) { string t_msgname=t_basemsg.m_msgName; string t_jsondata = _jsonmsg; switch (t_msgname) { case "N_msg_userload"://客服端向服务器发送登陆请求 new Task(() => { S_server_do.fns_load(t_jsondata, mi_sendBackClient); }).Start(); break; case "N_msg_userExit"://登陆的客户端告知服务器,退出 new Task(() => { S_server_do.fns_clientExit(t_jsondata, mi_sendBackClient); }).Start(); break; case "N_msg_studentExit"://登陆的学员退出登录 new Task(() => { S_server_do.fns_studentExit(t_jsondata, mi_sendBackClient); }).Start(); break; //------------------------------------------- case "N_msg_loadStart"://学员登陆成功后,写入登陆记录 new Task(() => { S_server_do.fns_loadStart(t_jsondata, mi_sendBackClient); }).Start(); break; case "N_msg_loadEnd"://退出登录时记录退出 new Task(() => { S_server_do.fns_loadEnd(t_jsondata, mi_sendBackClient); }).Start(); break; //---------------------------------------------- //------------------------------------------------ case "N_msg_study_start"://学习场景开始记录 new Task(() => { S_server_do.fns_SPT_start(t_jsondata, mi_sendBackClient,"study_load_1"); }).Start(); break; case "N_msg_study_end"://学习场景记录结束请求 new Task(() => { S_server_do.fns_SPT_end(t_jsondata, "study_load_1"); }).Start(); break; //------------------------------------------------ case "N_msg_prac_start"://练习场景开始记录 new Task(() => { S_server_do.fns_SPT_start(t_jsondata, mi_sendBackClient, "practise_load_1"); }).Start(); break; case "N_msg_prac_end"://练习场景结束记录 new Task(() => { S_server_do.fns_SPT_end(t_jsondata, "practise_load_1"); }).Start(); break; case "N_msg_prac_step"://练习场景的详细步骤记录 new Task(() => { S_server_do.fns_PT_stepRecord(t_jsondata, "practise_record_1"); }).Start(); break; //------------------------------------------------- case "N_msg_test_start"://考试场景开始记录 new Task(() => { S_server_do.fns_SPT_start(t_jsondata, mi_sendBackClient, "test_load_1"); }).Start(); break; case "N_msg_test_end"://考试场景结束记录 new Task(() => { S_server_do.fns_SPT_end(t_jsondata, "test_load_1"); }).Start(); break; case "N_msg_test_step"://考试场景的详细步骤记录 new Task(() => { S_server_do.fns_PT_stepRecord(t_jsondata, "test_record_1"); }).Start(); break; default: break; } } }
/// <summary> /// 接收消息处理,这里只是识别是否是正确的客服端使用 /// 如果符合要求,建立连接 /// </summary> /// <param name="传入收到的套接字"></param> private void fn_receivedMsg(Socket _clientSocket) { if (m_stopServer == true) { Console.WriteLine("服务器已经停止"); return; } Socket t_clientSoket = _clientSocket; if (t_clientSoket == null) { return; } bool t_waitClientID = true; byte[] t_result = new byte[1024]; while (t_waitClientID) { try { //把收到的byte数组转成字符串 int t_receivedNumber = t_clientSoket.Receive(t_result); string t_message = Encoding.UTF8.GetString(t_result, 0, t_receivedNumber); //把字符串转成想要的对象 A_baseMsg t_basemsg = JsonMapper.ToObject <A_baseMsg>(t_message); if (t_basemsg != null) { if (t_basemsg.m_msgName == "N_msg_helloToServer") { N_msg_helloToServer t_user = JsonMapper.ToObject <N_msg_helloToServer>(t_message); if (t_user.m_key == "hello,server,xreal,XREAL,0322") { //链接服务器成功 t_waitClientID = false; //结束循环 //加入到客服端集合中 S_ClientSocket.M_instance.fn_addSokect(t_clientSoket); //加入成功,反馈给客服端已经链接成功 N_msg_helloToServer_back t_back = new N_msg_helloToServer_back(true); string t_jsonback = JsonMapper.ToJson(t_back); t_clientSoket.Send(Encoding.UTF8.GetBytes(t_jsonback)); } else { N_msg_helloToServer_back t_back = new N_msg_helloToServer_back(false); string t_jsonback = JsonMapper.ToJson(t_back); t_clientSoket.Send(Encoding.UTF8.GetBytes(t_jsonback)); t_waitClientID = false; //结束循环 t_clientSoket.Close(); t_clientSoket.Dispose(); } } } else { Console.WriteLine("传入对象不能转换成正确的消息类"); t_waitClientID = false; //结束循环 } } catch (Exception _e) { t_waitClientID = false; //Console.WriteLine(_e.Message); t_clientSoket.Close(); t_clientSoket.Dispose(); //throw; } } //Console.WriteLine("--》"); //Console.ReadKey(); }