/// <summary> /// 加入广播消息 /// </summary> /// <returns></returns> public static void AddToMsgPool(string bar, object message) { // 加入广播消息 msgPool.Add(new SocketMessage() { RoomID = bar, Message = SerializeUtility.JavaScriptSerialize(message), }); isClear = false; }
/// <summary> /// 离开房间事件 /// </summary> /// <param name="bar"></param> /// <param name="ui"></param> /// <param name="un"></param> public static void ActionLeftRoom(string bar, string ui, string un) { var msg = new ActionModel <object>() { action = "leftroom", data = new { ui = ui, un = un, } }; // 保存至数据库 BarMessageHelper.Add(new BarMessageModel() { BarID = long.Parse(bar), Content = SerializeUtility.JavaScriptSerialize(msg), CreateTime = GetTimeMilliseconds(DateTime.Now), UserID = ui, Type = EnumBarMessageType.系统消息 }); AddToMsgPool(bar, msg); }
public static void DealMessage(string message, Socket client) { // 数据解析 var action = SerializeUtility.JavaScriptDeserialize <ActionModel <object> >(message); if (action != null) { switch (action.action) { case "login": { // 用户登录 var joinroommodel = SerializeUtility.JavaScriptDeserialize <ActionModel <LoginModel> >(message); clientPool[client].NickName = joinroommodel.data.un; clientPool[client].UserID = joinroommodel.data.ui; // 返回登录成功消息 SendMessage(client, SerializeUtility.JavaScriptSerialize( new ActionModel <object>() { action = "loginsuccess", data = new { } })); break; } case "joinroom": { // 加入房间,向当前用户发送加入成功消息 var joinroommodel = SerializeUtility.JavaScriptDeserialize <ActionModel <JoinRoomModel> >(message); // 获取房间信息 roomPool.TryAdd(joinroommodel.data.bar, new RoomInfo() { ID = joinroommodel.data.bar }); roomPool[joinroommodel.data.bar].UserList.AddOrPeplace(client, clientPool[client]); clientPool[client].Bar = joinroommodel.data.bar; // 返回房间加入成功消息 SendMessage(client, SerializeUtility.JavaScriptSerialize( new ActionModel <object>() { action = "joinroomsuccess", data = new { } })); var actmodel = new ActionModel <object>() { action = "joinroom", data = new { ui = joinroommodel.data.ui, un = joinroommodel.data.un, } }; // 保存至数据库 BarMessageHelper.Add(new BarMessageModel() { BarID = long.Parse(joinroommodel.data.bar), Content = SerializeUtility.JavaScriptSerialize(actmodel), CreateTime = GetTimeMilliseconds(DateTime.Now), UserID = joinroommodel.data.ui, Type = EnumBarMessageType.系统消息 }); AddToMsgPool(joinroommodel.data.bar, actmodel); break; } case "sendmessage": { var messagemodel = SerializeUtility.JavaScriptDeserialize <ActionModel <MessageModel> >(message); var actmodel = new ActionModel <object>() { action = "newmessage", data = new { ui = messagemodel.data.ui, un = messagemodel.data.un, msg = messagemodel.data.msg, } }; // 保存至数据库 BarMessageHelper.Add(new BarMessageModel() { BarID = long.Parse(messagemodel.data.bar), Content = SerializeUtility.JavaScriptSerialize(actmodel), CreateTime = GetTimeMilliseconds(DateTime.Now), UserID = messagemodel.data.ui, Type = EnumBarMessageType.文字消息 }); AddToMsgPool(messagemodel.data.bar, actmodel); break; } } } }