TServerC.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using UnityEngine;
  2. using System.Collections;
  3. //引入库
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using LitJson;
  9. public class TServerC
  10. {
  11. public TServer ts;
  12. public int id;
  13. public Socket _socket;
  14. Thread connectThread; //连接线程
  15. byte[] recvData;
  16. byte[] sendData;
  17. int recvLen;
  18. string recvStr;
  19. string sname;
  20. bool isSpeak = false;
  21. bool isMute = false;
  22. public JsonData getInfo()
  23. {
  24. JsonData data = new JsonData();
  25. data["nickname"] = sname;
  26. data["socketId"] = id;
  27. data["canSpeek"] = isMute;
  28. data["isSpeak"] = isSpeak;
  29. return data;
  30. }
  31. public void init()
  32. {
  33. //开启一个线程连接,必须的,否则主线程卡死
  34. connectThread = new Thread(new ThreadStart(SocketReceive));
  35. connectThread.Start();
  36. }
  37. public static int bytes2Int(byte[] b)
  38. {
  39. int i0 = b[0] & 0xFF; //将最后的8位转给了int类型的最后的8位
  40. int i1 = (b[1] & 0xFF) << 8; //我们之所以先与 0xFF 不能直接 或运算,因为在进行运算时,会转为int类型,32位。如果是负数,前面就会补位全为1.也就不能算出正确的值了。所以,我们先与 (前面会有24个0)11111111,让高位保持为0.以保证后面运算的正确性
  41. int i2 = (b[2] & 0xFF) << 16;
  42. int i3 = (b[3] & 0xFF) << 24;
  43. return i0 | i1 | i2 | i3;
  44. }
  45. //服务器接收
  46. void SocketReceive()
  47. {
  48. while (true)
  49. {
  50. byte[] recvBytes = new byte[4];
  51. int bodyLength = _socket.Receive(recvBytes, 0, recvBytes.Length, SocketFlags.None);
  52. int baotou = bytes2Int(recvBytes);
  53. Debug.Log("准备接收==>" + baotou);
  54. //对data清零
  55. recvData = new byte[baotou];
  56. //获取收到的数据的长度
  57. recvLen = 0;
  58. while (recvLen != recvData.Length)
  59. {
  60. // Debug.Log("准备接收==>" + recvLen);
  61. recvLen += _socket.Receive(recvData, recvLen, recvData.Length - recvLen, SocketFlags.None);
  62. // Debug.Log("server 接收到Client 长度==>" + recvLen);
  63. }
  64. //如果收到的数据长度为0,则重连并进入下一个循环
  65. if (recvLen == 0)
  66. {
  67. _socket.Close();
  68. ts.clientSockets.Remove(this);
  69. Debug.Log("Client==>" + id +"断开连接=====》");
  70. // SocketConnet();
  71. break ;
  72. }
  73. //输出接收到的数据
  74. recvStr = Encoding.ASCII.GetString(recvData, 0, recvLen);
  75. //将接收到的数据经过处理再发送出去
  76. // sendStr = "From Server: " + recvStr;
  77. //SocketSend(sendStr);
  78. Debug.Log("server 接收到Client==>" + id + "的信息=====》"+ recvStr);
  79. JsonData data = JsonMapper.ToObject(recvStr) ;
  80. messageData(data);
  81. }
  82. }
  83. public void messageData(JsonData data)
  84. {
  85. TServerC tsc;
  86. JsonData fdata;
  87. switch (data["action"].ToString())
  88. {
  89. case "LoginByToken":
  90. sname = data["data"]["token"].ToString();
  91. break;
  92. case "CreateRoom":
  93. ts.ownerName = id.ToString();
  94. fdata = new JsonData();
  95. fdata["type"] = "Room";
  96. fdata["data"] = new JsonData();
  97. fdata["data"]["method"] = "Create";
  98. fdata["data"]["roomId"] = "0001";
  99. fdata["data"]["ownerName"] = ts.ownerName;
  100. fdata["data"]["RoomUserList"] = ts.getRoomList(id);
  101. fdata["data"]["message"] = "创建房间成功";
  102. ts.SocketSend(fdata.ToJson(), -1);
  103. break;
  104. case "JoinRoom":
  105. fdata = new JsonData();
  106. fdata["type"] = "Room";
  107. fdata["data"] = new JsonData();
  108. fdata["data"]["method"] = "UserJoin";
  109. fdata["data"]["roomId"] = "0001";
  110. fdata["data"]["socketId"] = id;
  111. fdata["data"]["nickname"] = id;
  112. fdata["data"]["message"] = "用户进入房间";
  113. ts.SocketSend(fdata.ToJson(), id);
  114. fdata = new JsonData();
  115. fdata["type"] = "Room";
  116. fdata["data"] = new JsonData();
  117. fdata["data"]["method"] = "Join";
  118. fdata["data"]["roomId"] = "0001";
  119. fdata["data"]["roomName"] = "0001";
  120. fdata["data"]["ownerName"] = ts.ownerName;
  121. fdata["data"]["RoomUserList"] = ts.getRoomList(id);
  122. fdata["data"]["message"] = "进入房间";
  123. SocketSend(fdata.ToJson());
  124. break;
  125. case "LeaveRoom":
  126. fdata = new JsonData();
  127. fdata["type"] = "UserLogout";
  128. fdata["data"] = new JsonData();
  129. fdata["data"]["socketId"] = id;
  130. fdata["data"]["nickname"] = id;
  131. fdata["data"]["message"] = "用户登出";
  132. ts.SocketSend(fdata.ToJson(), -1);
  133. _socket.Close();
  134. ts.clientSockets.Remove(this);
  135. break;
  136. case "IceCandidate":
  137. tsc = ts.getRoomSocketById(int.Parse(data["data"]["socketId"].ToString()));
  138. fdata = new JsonData();
  139. fdata["type"] = "IceCandidate";
  140. fdata["data"] = new JsonData();
  141. fdata["data"]["method"] = "IceCandidate";
  142. fdata["data"]["label"] = data["data"]["label"].ToString();
  143. fdata["data"]["candidate"] = data["data"]["candidate"].ToString();
  144. fdata["data"]["id"] = data["data"]["id"].ToString();
  145. fdata["data"]["socketId"] = id;
  146. tsc.SocketSend(fdata.ToJson());
  147. break;
  148. case "Offer":
  149. tsc = ts.getRoomSocketById(int.Parse(data["data"]["socketId"].ToString()));
  150. fdata = new JsonData();
  151. fdata["type"] = "Offer";
  152. fdata["data"] = new JsonData();
  153. fdata["data"]["method"] = "Offer";
  154. fdata["data"]["sdp"] = data["data"]["sdp"];
  155. fdata["data"]["socketId"] = id;
  156. tsc.SocketSend(fdata.ToJson());
  157. break;
  158. case "Answer":
  159. tsc = ts.getRoomSocketById(int.Parse(data["data"]["socketId"].ToString()));
  160. fdata = new JsonData();
  161. fdata["type"] = "Answer";
  162. fdata["data"] = new JsonData();
  163. fdata["data"]["method"] = "Answer";
  164. fdata["data"]["sdp"] = data["data"]["sdp"];
  165. fdata["data"]["socketId"] = id;
  166. tsc.SocketSend(fdata.ToJson());
  167. break;
  168. case "sendInfo":
  169. fdata = new JsonData();
  170. fdata["type"] = "Room";
  171. fdata["data"] = new JsonData();
  172. fdata["data"]["method"] = "sendInfo";
  173. fdata["data"]["roomList"] = data["data"]["info"].ToString();
  174. fdata["data"]["message"] = "用户进入房间";
  175. ts.SocketSend(fdata.ToJson(), id);
  176. break;
  177. case "csj":
  178. fdata = new JsonData();
  179. fdata["type"] = "Room";
  180. fdata["data"] = new JsonData();
  181. fdata["data"]["method"] = "csj";
  182. fdata["data"]["roomList"] = data["data"]["info"].ToString();
  183. ts.SocketSend(fdata.ToJson(), id);
  184. break;
  185. case "firstView":
  186. fdata = new JsonData();
  187. fdata["type"] = "View";
  188. fdata["data"] = new JsonData();
  189. fdata["data"]["method"] = "firstView";
  190. fdata["data"]["pose"] = data["data"]["info"].ToString();
  191. ts.SocketSend(fdata.ToJson(), id);
  192. break;
  193. case "thirdView":
  194. fdata = new JsonData();
  195. fdata["type"] = "View";
  196. fdata["data"] = new JsonData();
  197. fdata["data"]["method"] = "thirdView";
  198. fdata["data"]["pose"] = data["data"]["info"].ToString();
  199. ts.SocketSend(fdata.ToJson(), id);
  200. break;
  201. }
  202. }
  203. public void SocketSend(string sendStr)
  204. {
  205. //清空发送缓存
  206. sendData = new byte[1024];
  207. //数据类型转换
  208. sendData = Encoding.ASCII.GetBytes(sendStr);
  209. byte[] bb = TClient.int2Bytes(sendData.Length);
  210. _socket.Send(bb, bb.Length, SocketFlags.None);
  211. Debug.Log("Client发送的信息给服务器=====》长度" + sendData.Length);
  212. //发送
  213. _socket.Send(sendData, sendData.Length, SocketFlags.None);
  214. Debug.Log("server 向Client==>" + id + "发送的信息=====》" + sendStr);
  215. }
  216. }