• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共151篇

    Unity - 游戏引擎

关闭

返回栏目

关闭

返回Unity - 游戏引擎栏目

105 - NET - ClientManager.cs - 客户端连接器

作者:

贺及楼

成为作者

更新日期:2023-09-17 10:58:34

  1. /// <summary>
  2. /// 这个是用来管理跟服务器端的Socket连接
  3. /// </summary>
  4. public class ClientManager :BaseManager {
  5. private const string IP = "127.0.0.1";
  6. private const int PORT = 6688;
  7. private Socket clientSocket;
  8. //实例化信息
  9. private Message msg = new Message();
  10. public ClientManager(GameFacade facade) : base(facade) { }
  11. //连接服务器
  12. public override void OnInit(){}
  13. //监听
  14. private void Start(){}
  15. //接收数据
  16. private void ReceiveCallback(IAsyncResult ar){}
  17. private void OnProcessDataCallback(动作码,数据){}
  18. //发送请求
  19. public void SendRequest(请求码,动作码,数据){}
  20. //关闭游戏
  21. public override void OnDestroy(){}
  1. /// <summary>
  2. /// 这个是用来管理跟服务器端的Socket连接
  3. /// </summary>
  4. public class ClientManager :BaseManager {
  5. private const string IP = "127.0.0.1";
  6. private const int PORT = 6688;
  7. private Socket clientSocket;
  8. //实例化信息
  9. private Message msg = new Message();
  10. public ClientManager(GameFacade facade) : base(facade) { }
  11. //连接服务器
  12. public override void OnInit(){}
  13. //监听
  14. private void Start(){}
  15. //接收数据
  16. private void ReceiveCallback(IAsyncResult ar){}
  17. private void OnProcessDataCallback(ActionCode actionCode,string data){}
  18. //发送请求
  19. public void SendRequest(RequestCode requestCode, ActionCode actionCode, string data){}
  20. //关闭游戏
  21. public override void OnDestroy(){}
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Net.Sockets;
  5. using System;
  6. using Common;
  7. /// <summary>
  8. /// 这个是用来管理跟服务器端的Socket连接
  9. /// </summary>
  10. public class ClientManager :BaseManager {
  11. private const string IP = "127.0.0.1";
  12. private const int PORT = 6688;
  13. private Socket clientSocket;
  14. //实例化信息
  15. private Message msg = new Message();
  16. public ClientManager(GameFacade facade) : base(facade) { }
  17. //连接服务器
  18. public override void OnInit()
  19. {
  20. base.OnInit();
  21. clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  22. try
  23. {
  24. clientSocket.Connect(IP, PORT);
  25. Start();
  26. }
  27. catch (Exception e)
  28. {
  29. Debug.LogWarning("无法连接到服务器端,请检查您的网络!!" + e);
  30. }
  31. }
  32. //监听
  33. private void Start()
  34. {
  35. clientSocket.BeginReceive(msg.Data,msg.StartIndex,msg.RemainSize, SocketFlags.None, ReceiveCallback, null);
  36. }
  37. //接收数据
  38. private void ReceiveCallback(IAsyncResult ar)
  39. {
  40. try
  41. {
  42. if (clientSocket == null || clientSocket.Connected == false) return;
  43. int count = clientSocket.EndReceive(ar);
  44. msg.ReadMessage(count, OnProcessDataCallback);
  45. Start();
  46. }
  47. catch(Exception e)
  48. {
  49. Debug.Log(e);
  50. }
  51. }
  52. private void OnProcessDataCallback(ActionCode actionCode,string data)
  53. {
  54. facade.HandleReponse(actionCode, data);
  55. }
  56. //发送请求
  57. public void SendRequest(RequestCode requestCode, ActionCode actionCode, string data)
  58. {
  59. byte[] bytes = Message.PackData(requestCode, actionCode, data);
  60. clientSocket.Send(bytes);
  61. }
  62. //关闭游戏
  63. public override void OnDestroy()
  64. {
  65. base.OnDestroy();
  66. try
  67. {
  68. clientSocket.Close();
  69. }catch(Exception e)
  70. {
  71. Debug.LogWarning("无法关闭跟服务器端的连接!!" + e);
  72. }
  73. }
  74. }