修改对战房间指令无反应的问题

This commit is contained in:
lishuo 2024-05-10 16:32:10 +08:00
parent b84409a5fd
commit 6e6d3b2050
2 changed files with 8 additions and 13 deletions

View File

@ -38,7 +38,6 @@ namespace OnlineUserPool.Services
if (session is { IsConnected: true }) if (session is { IsConnected: true })
{ {
session.SendAsync(dgram); session.SendAsync(dgram);
dgram = null;
} }
} }
@ -69,8 +68,7 @@ namespace OnlineUserPool.Services
{ {
try try
{ {
var item = this.Sessions.Values.FirstOrDefault(t => var item = this.Sessions.Values.FirstOrDefault(t => t.Socket.Connected && t.Socket.RemoteEndPoint.ToString() == ip.ToString());
t.Socket.Connected && t.Socket.RemoteEndPoint.ToString() == ip.ToString());
return item; return item;
} }
catch (Exception e) catch (Exception e)
@ -122,17 +120,17 @@ namespace OnlineUserPool.Services
var data = string.Empty; var data = string.Empty;
try try
{ {
this._dataBuffer.Clear();
data = Encoding.UTF8.GetString(buffer, (int)offset, (int)size); data = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
foreach (var item in data) foreach (var item in data)
{ {
this._dataBuffer.Append(item); this._dataBuffer.Append(item);
if (item != '}') continue; if (item != '}') continue;
Debug.WriteLine("收到消息:" + this._dataBuffer);
var s = this._dataBuffer.ToString(); var s = this._dataBuffer.ToString();
var msg = JsonConvert.DeserializeObject<ReceiveModel>(s); var msg = JsonConvert.DeserializeObject<ReceiveModel>(s);
var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString()); var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString());
switch (msg.CommandType) switch (msg.CommandType)
{ {
case 1: case 1:
_action(ipEndPoint, JsonConvert.DeserializeObject<MsgModel>(s)); _action(ipEndPoint, JsonConvert.DeserializeObject<MsgModel>(s));
break; break;
@ -170,23 +168,20 @@ namespace OnlineUserPool.Services
_action(ipEndPoint, msg); _action(ipEndPoint, msg);
break; break;
} }
this._dataBuffer.Clear(); this._dataBuffer.Clear();
s = null;
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Log.Error($"接受到的数据处理时出错:{data}\r\n{ex.Message}\r\n{ex.StackTrace}"); Log.Error($"接受到的数据处理时出错:{data}\r\n{ex.Message}\r\n{ex.StackTrace}");
} }
buffer = null;
data = null;
} }
protected override void OnError(SocketError error) protected override void OnError(SocketError error)
{ {
//base.OnError(error); //base.OnError(error);
Debug.WriteLine(error); Log.Error($"OnError{error}");
} }
} }
} }

View File

@ -289,7 +289,7 @@ namespace OnlineUserPool.ViewModels
int total = 0; int total = 0;
var G = $"{total}l[{gameRoom},detail{{{ss}}}]"; var G = $"{total}l[{gameRoom},detail{{{ss}}}]";
var temp = string.Join("|", list.Select(m => m.ToString(2))) + "|"; var temp = string.Join("|", list.Select(m => m.ToString(2))) + "|";
var strV21 = $"*l{{{ temp }}};g{{{ G}}};#"; var strV21 = $"l{{{ temp }}};g{{{ G}}};";
var ddd = new NetworkData(strV21); var ddd = new NetworkData(strV21);
var needList = Clients.Where(c => c.RoomId == gameRoom.RoomId).ToList(); var needList = Clients.Where(c => c.RoomId == gameRoom.RoomId).ToList();
@ -316,7 +316,7 @@ namespace OnlineUserPool.ViewModels
foreach (var item in needlist) foreach (var item in needlist)
{ {
var G = GameRoomMessageHandler(item); var G = GameRoomMessageHandler(item);
var strV21 = $"*l{{{ temp }}};g{{{G}}};#"; var strV21 = $"l{{{ temp }}};g{{{G}}};";
var ddd = new NetworkData(strV21); var ddd = new NetworkData(strV21);
bool isZip = item.Encoding == "gzip"; bool isZip = item.Encoding == "gzip";
item.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, item.IPEndPoint); item.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, item.IPEndPoint);
@ -369,7 +369,7 @@ namespace OnlineUserPool.ViewModels
roomInfo += $"[{r.ToString()},detail{{{ss}}}]"; roomInfo += $"[{r.ToString()},detail{{{ss}}}]";
} }
var strV21 = $"*l{{{ temp }}};g{{{roomInfo}}};#"; var strV21 = $"l{{{ temp }}};g{{{roomInfo}}};";
var ddd = new NetworkData(strV21); var ddd = new NetworkData(strV21);
bool isZip = needSend.Encoding == "gzip"; bool isZip = needSend.Encoding == "gzip";
needSend.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, needSend.IPEndPoint); needSend.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, needSend.IPEndPoint);