public void OnReceivePlayerInfo(NetSCSelfInfoMsg msg) { if (GameMain.instance.OfflineModel) { return; } PlayerManager.Instance.CreateHostPlayer(msg.PlayerId); }
public static void OnConnected(IAsyncResult ar) { var socket = tcpSocket.EndAccept(ar); //新建一个角色 PlayerInfo playerInfo = new PlayerInfo(); playerInfo.Socket = socket; playerInfo.PlayerId = curPlayerId++; playerInfo.IsOperationReceived = true; //第一帧是服务器直接发过去的空帧 playerList.Add(playerInfo); Console.WriteLine("收到客户端连接,玩家id = " + playerInfo.PlayerId); socket.BeginReceive(playerInfo.ReadBuffer, 0, playerInfo.ReadBuffer.Length, SocketFlags.None, OnReceive, playerInfo); //发送玩家信息 netSendBuff.Reset(); NetSCSelfInfoMsg msg = new NetSCSelfInfoMsg(); msg.PlayerId = playerInfo.PlayerId; msg.Write(netSendBuff); socket.Send(netSendBuff.DataBuffer); //玩家列表消息 NetSCPlayerListMsg msg2 = new NetSCPlayerListMsg(); for (int i = 0; i < playerList.Count; i++) { msg2.PlayerList.PlayerList.Add(playerList[i].PlayerId); } netSendBuff.Reset(); msg2.Write(netSendBuff); //给所有玩家发送玩家列表 for (int i = 0; i < playerList.Count; i++) { try { playerList[i].Socket.Send(netSendBuff.DataBuffer); } catch (Exception e) { playerList[i].IsDisconnected = true; } } tcpSocket.BeginAccept(OnConnected, null); }