WebRpcImplementationExample.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="WebRpcImplementationExample.cs" company="Exit Games GmbH">
  3. // PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // Sample of best practices when implementing & handling WebRPCs.
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // ----------------------------------------------------------------------------
  10. using ExitGames.Client.Photon;
  11. using Photon.Realtime;
  12. using UnityEngine;
  13. namespace Photon.Pun.Demo
  14. {
  15. /// <summary>
  16. /// This class is a sample of how to implement WebRPCs calling & callbacks.
  17. /// </summary>
  18. public class WebRpcImplementationExample : MonoBehaviour, IWebRpcCallback
  19. {
  20. /// <summary>
  21. /// example of WebRPC method name, add yours as enum or constants to avoid typos and have them in one place
  22. /// </summary>
  23. public const string GetGameListWebRpcMethodName = "GetGameList";
  24. public void OnWebRpcResponse(OperationResponse response)
  25. {
  26. Debug.LogFormat("WebRPC operation response {0}", response.ToStringFull());
  27. switch (response.ReturnCode)
  28. {
  29. case ErrorCode.Ok:
  30. WebRpcResponse webRpcResponse = new WebRpcResponse(response);
  31. Debug.LogFormat("Parsed WebRPC response {0}", response.ToStringFull());
  32. if (string.IsNullOrEmpty(webRpcResponse.Name))
  33. {
  34. Debug.LogError("Unexpected: WebRPC response did not contain WebRPC method name");
  35. }
  36. if (webRpcResponse.ResultCode == 0) // success
  37. {
  38. switch (webRpcResponse.Name)
  39. {
  40. // todo: add your code here
  41. case GetGameListWebRpcMethodName: // example
  42. // ...
  43. break;
  44. }
  45. }
  46. else if (webRpcResponse.ResultCode == -1)
  47. {
  48. Debug.LogErrorFormat("Web server did not return ResultCode for WebRPC method=\"{0}\", Message={1}", webRpcResponse.Name, webRpcResponse.Message);
  49. }
  50. else
  51. {
  52. Debug.LogErrorFormat("Web server returned ResultCode={0} for WebRPC method=\"{1}\", Message={2}", webRpcResponse.ResultCode, webRpcResponse.Name, webRpcResponse.Message);
  53. }
  54. break;
  55. case ErrorCode.ExternalHttpCallFailed: // web service unreachable
  56. Debug.LogErrorFormat("WebRPC call failed as request could not be sent to the server. {0}", response.DebugMessage);
  57. break;
  58. case ErrorCode.HttpLimitReached: // too many WebRPCs in a short period of time
  59. // the debug message should contain the limit exceeded
  60. Debug.LogErrorFormat("WebRPCs rate limit exceeded: {0}", response.DebugMessage);
  61. break;
  62. case ErrorCode.InvalidOperation: // WebRPC not configured at all OR not configured properly OR trying to send on name server
  63. if (PhotonNetwork.Server == ServerConnection.NameServer)
  64. {
  65. Debug.LogErrorFormat("WebRPC not supported on NameServer. {0}", response.DebugMessage);
  66. }
  67. else
  68. {
  69. Debug.LogErrorFormat("WebRPC not properly configured or not configured at all. {0}", response.DebugMessage);
  70. }
  71. break;
  72. default:
  73. // other unknown error, unexpected
  74. Debug.LogErrorFormat("Unexpected error, {0} {1}", response.ReturnCode, response.DebugMessage);
  75. break;
  76. }
  77. }
  78. public void WebRpcExampleCall()
  79. {
  80. WebRpcCall(GetGameListWebRpcMethodName);
  81. }
  82. public static void WebRpcCall(string methodName, object parameters = null, bool sendAuthCookieIfAny = false)
  83. {
  84. if (string.IsNullOrEmpty(methodName))
  85. {
  86. Debug.LogError("WebRpc method name must not be null nor empty");
  87. return;
  88. }
  89. if (!PhotonNetwork.WebRpc(methodName, parameters, sendAuthCookieIfAny))
  90. {
  91. Debug.LogErrorFormat("Error sending WebRPC \"{0}\" (\"{1}\") request, check the previous error logs for more details", methodName, parameters);
  92. }
  93. }
  94. private void OnEnable()
  95. {
  96. PhotonNetwork.AddCallbackTarget(this);
  97. }
  98. private void OnDisable()
  99. {
  100. PhotonNetwork.RemoveCallbackTarget(this);
  101. }
  102. }
  103. }