using UnityEngine; using System.Collections; //引入库 using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using LitJson; public class TServerC { public TServer ts; public int id; public Socket _socket; Thread connectThread; //连接线程 byte[] recvData; byte[] sendData; int recvLen; string recvStr; string sname; bool isSpeak = false; bool isMute = false; public JsonData getInfo() { JsonData data = new JsonData(); data["nickname"] = sname; data["socketId"] = id; data["canSpeek"] = isMute; data["isSpeak"] = isSpeak; return data; } public void init() { //开启一个线程连接,必须的,否则主线程卡死 connectThread = new Thread(new ThreadStart(SocketReceive)); connectThread.Start(); } public static int bytes2Int(byte[] b) { int i0 = b[0] & 0xFF; //将最后的8位转给了int类型的最后的8位 int i1 = (b[1] & 0xFF) << 8; //我们之所以先与 0xFF 不能直接 或运算,因为在进行运算时,会转为int类型,32位。如果是负数,前面就会补位全为1.也就不能算出正确的值了。所以,我们先与 (前面会有24个0)11111111,让高位保持为0.以保证后面运算的正确性 int i2 = (b[2] & 0xFF) << 16; int i3 = (b[3] & 0xFF) << 24; return i0 | i1 | i2 | i3; } //服务器接收 void SocketReceive() { while (true) { byte[] recvBytes = new byte[4]; int bodyLength = _socket.Receive(recvBytes, 0, recvBytes.Length, SocketFlags.None); int baotou = bytes2Int(recvBytes); Debug.Log("准备接收==>" + baotou); //对data清零 recvData = new byte[baotou]; //获取收到的数据的长度 recvLen = 0; while (recvLen != recvData.Length) { // Debug.Log("准备接收==>" + recvLen); recvLen += _socket.Receive(recvData, recvLen, recvData.Length - recvLen, SocketFlags.None); // Debug.Log("server 接收到Client 长度==>" + recvLen); } //如果收到的数据长度为0,则重连并进入下一个循环 if (recvLen == 0) { _socket.Close(); ts.clientSockets.Remove(this); Debug.Log("Client==>" + id +"断开连接=====》"); // SocketConnet(); break ; } //输出接收到的数据 recvStr = Encoding.ASCII.GetString(recvData, 0, recvLen); //将接收到的数据经过处理再发送出去 // sendStr = "From Server: " + recvStr; //SocketSend(sendStr); Debug.Log("server 接收到Client==>" + id + "的信息=====》"+ recvStr); JsonData data = JsonMapper.ToObject(recvStr) ; messageData(data); } } public void messageData(JsonData data) { TServerC tsc; JsonData fdata; switch (data["action"].ToString()) { case "LoginByToken": sname = data["data"]["token"].ToString(); break; case "CreateRoom": ts.ownerName = id.ToString(); fdata = new JsonData(); fdata["type"] = "Room"; fdata["data"] = new JsonData(); fdata["data"]["method"] = "Create"; fdata["data"]["roomId"] = "0001"; fdata["data"]["ownerName"] = ts.ownerName; fdata["data"]["RoomUserList"] = ts.getRoomList(id); fdata["data"]["message"] = "创建房间成功"; ts.SocketSend(fdata.ToJson(), -1); break; case "JoinRoom": fdata = new JsonData(); fdata["type"] = "Room"; fdata["data"] = new JsonData(); fdata["data"]["method"] = "UserJoin"; fdata["data"]["roomId"] = "0001"; fdata["data"]["socketId"] = id; fdata["data"]["nickname"] = id; fdata["data"]["message"] = "用户进入房间"; ts.SocketSend(fdata.ToJson(), id); fdata = new JsonData(); fdata["type"] = "Room"; fdata["data"] = new JsonData(); fdata["data"]["method"] = "Join"; fdata["data"]["roomId"] = "0001"; fdata["data"]["roomName"] = "0001"; fdata["data"]["ownerName"] = ts.ownerName; fdata["data"]["RoomUserList"] = ts.getRoomList(id); fdata["data"]["message"] = "进入房间"; SocketSend(fdata.ToJson()); break; case "LeaveRoom": fdata = new JsonData(); fdata["type"] = "UserLogout"; fdata["data"] = new JsonData(); fdata["data"]["socketId"] = id; fdata["data"]["nickname"] = id; fdata["data"]["message"] = "用户登出"; ts.SocketSend(fdata.ToJson(), -1); _socket.Close(); ts.clientSockets.Remove(this); break; case "IceCandidate": tsc = ts.getRoomSocketById(int.Parse(data["data"]["socketId"].ToString())); fdata = new JsonData(); fdata["type"] = "IceCandidate"; fdata["data"] = new JsonData(); fdata["data"]["method"] = "IceCandidate"; fdata["data"]["label"] = data["data"]["label"].ToString(); fdata["data"]["candidate"] = data["data"]["candidate"].ToString(); fdata["data"]["id"] = data["data"]["id"].ToString(); fdata["data"]["socketId"] = id; tsc.SocketSend(fdata.ToJson()); break; case "Offer": tsc = ts.getRoomSocketById(int.Parse(data["data"]["socketId"].ToString())); fdata = new JsonData(); fdata["type"] = "Offer"; fdata["data"] = new JsonData(); fdata["data"]["method"] = "Offer"; fdata["data"]["sdp"] = data["data"]["sdp"]; fdata["data"]["socketId"] = id; tsc.SocketSend(fdata.ToJson()); break; case "Answer": tsc = ts.getRoomSocketById(int.Parse(data["data"]["socketId"].ToString())); fdata = new JsonData(); fdata["type"] = "Answer"; fdata["data"] = new JsonData(); fdata["data"]["method"] = "Answer"; fdata["data"]["sdp"] = data["data"]["sdp"]; fdata["data"]["socketId"] = id; tsc.SocketSend(fdata.ToJson()); break; case "sendInfo": fdata = new JsonData(); fdata["type"] = "Room"; fdata["data"] = new JsonData(); fdata["data"]["method"] = "sendInfo"; fdata["data"]["roomList"] = data["data"]["info"].ToString(); fdata["data"]["message"] = "用户进入房间"; ts.SocketSend(fdata.ToJson(), id); break; case "csj": fdata = new JsonData(); fdata["type"] = "Room"; fdata["data"] = new JsonData(); fdata["data"]["method"] = "csj"; fdata["data"]["roomList"] = data["data"]["info"].ToString(); ts.SocketSend(fdata.ToJson(), id); break; case "firstView": fdata = new JsonData(); fdata["type"] = "View"; fdata["data"] = new JsonData(); fdata["data"]["method"] = "firstView"; fdata["data"]["pose"] = data["data"]["info"].ToString(); ts.SocketSend(fdata.ToJson(), id); break; case "thirdView": fdata = new JsonData(); fdata["type"] = "View"; fdata["data"] = new JsonData(); fdata["data"]["method"] = "thirdView"; fdata["data"]["pose"] = data["data"]["info"].ToString(); ts.SocketSend(fdata.ToJson(), id); break; } } public void SocketSend(string sendStr) { //清空发送缓存 sendData = new byte[1024]; //数据类型转换 sendData = Encoding.ASCII.GetBytes(sendStr); byte[] bb = TClient.int2Bytes(sendData.Length); _socket.Send(bb, bb.Length, SocketFlags.None); Debug.Log("Client发送的信息给服务器=====》长度" + sendData.Length); //发送 _socket.Send(sendData, sendData.Length, SocketFlags.None); Debug.Log("server 向Client==>" + id + "发送的信息=====》" + sendStr); } }