Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

socket.io, web app working http, but not https

Discussion in 'Multiplayer' started by omnedeus_unity, Mar 10, 2020.

  1. omnedeus_unity

    omnedeus_unity

    Joined:
    Apr 3, 2019
    Posts:
    7
    hello everyone!

    I thought I would enlist some help with my problem.

    I've created a golfing game which uses the webcam for IR codes. Therefore, we had to move the http server to https.

    Now when I play the game, I get connection errors:

    NullReferenceException: Object reference not set to an instance of an object
    socket.io.WebSocketTrigger.Update () (at Assets/Plugins/socket.io/WebSocketTrigger.cs:74)

    This error keeps getting repeated. I figured it was because a security certificate error was happening in-between my unity build, and the website.

    code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UniRx;
    3. using UniRx.Triggers;
    4. using System;
    5. using System.Linq;
    6. using System.Collections;
    7.  
    8.  
    9. namespace socket.io {
    10.  
    11.     /// <summary>
    12.     /// Streams out received packets as observable
    13.     /// </summary>
    14.     public class WebSocketTrigger : ObservableTriggerBase {
    15.  
    16.         /// <summary>
    17.         /// Observes received packets and also starts Ping-Pong routine
    18.         /// </summary>
    19.         /// <returns></returns>
    20.         public UniRx.IObservable<string> OnRecvAsObservable() {
    21.             if (_cancelPingPong == null) {
    22.                 _cancelPingPong = gameObject.UpdateAsObservable()
    23.                     .Sample(TimeSpan.FromSeconds(10f))
    24.                     .Subscribe(_ => {
    25.                         WebSocket.Send(Packet.Ping);
    26.                         Debug.LogFormat("socket.io => {0} ping~", WebSocket.Url.ToString());
    27.                     });
    28.             }
    29.  
    30.             if (_onRecv == null)
    31.                 _onRecv = new Subject<string>();
    32.  
    33.             return _onRecv;
    34.         }
    35.      
    36.         protected override void RaiseOnCompletedOnDestroy() {
    37.             if (_cancelPingPong != null) {
    38.                 _cancelPingPong.Dispose();
    39.                 _cancelPingPong = null;
    40.             }
    41.  
    42.             if (_onRecv != null) {
    43.                 _onRecv.OnCompleted();
    44.                 _onRecv = null;
    45.             }
    46.  
    47.             if (!IsConnected)
    48.                 WebSocket.Close();
    49.         }
    50.  
    51.         IDisposable _cancelPingPong;
    52.         Subject<string> _onRecv;
    53.  
    54.         /// <summary>
    55.         /// WebSocket object ref
    56.         /// </summary>
    57.         public WebSocketWrapper WebSocket { get; set; }
    58.  
    59.         /// <summary>
    60.         /// Holds the last error on WebSocket
    61.         /// </summary>
    62.         public string LastWebSocketError { get; private set; }
    63.  
    64.         public bool IsConnected {
    65.             get { return WebSocket != null && WebSocket.IsConnected; }
    66.         }
    67.  
    68.         public bool IsProbed { get; set; }
    69.  
    70.         public bool IsUpgraded { get; set; }
    71.      
    72.  
    73.         void Update() {
    74.             LastWebSocketError = WebSocket.GetLastError();
    75.  
    76.             if (!string.IsNullOrEmpty(LastWebSocketError)) {
    77.                 CheckAndHandleWebSocketDisconnect();
    78.                 Debug.LogError(LastWebSocketError);
    79.             }
    80.  
    81.             //print("------IsConnected----->>>" + IsConnected);
    82.             if (IsConnected)
    83.                 ReceiveWebSocketData();
    84.         }
    85.  
    86.         void ReceiveWebSocketData() {
    87.             var recvData = WebSocket.Recv();
    88.  
    89.             if (string.IsNullOrEmpty(recvData))
    90.                 return;
    91.  
    92.             if (recvData == Packet.ProbeAnswer) {
    93.                 IsProbed = true;
    94.                 Debug.LogFormat("socket.io => {0} probed~", WebSocket.Url.ToString());
    95.             }
    96.             else if (recvData == Packet.Pong) {
    97.                 Debug.LogFormat("socket.io => {0} pong~", WebSocket.Url.ToString());
    98.             }
    99.             else {
    100.                 if (_onRecv != null)
    101.                     _onRecv.OnNext(recvData);
    102.             }
    103.         }
    104.      
    105.         void CheckAndHandleWebSocketDisconnect() {
    106.             if (IsConnected)
    107.                 return;
    108.  
    109.             if (_onRecv != null) {
    110.                 _cancelPingPong.Dispose();
    111.                 _cancelPingPong = null;
    112.                 _onRecv.Dispose();
    113.                 _onRecv = null;
    114.                 IsProbed = false;
    115.                 IsUpgraded = false;
    116.  
    117.                 var sockets = gameObject.GetComponentsInChildren<Socket>();
    118.                 foreach (var s in sockets) {
    119.                     if (s.OnDisconnect != null)
    120.                         s.OnDisconnect();
    121.                 }
    122.             }
    123.          
    124.             if (SocketManager.Instance.Reconnection) {
    125.                 var sockets = gameObject.GetComponentsInChildren<Socket>();
    126.                 foreach (var s in sockets)
    127.                     SocketManager.Instance.Reconnect(s, 1);
    128.             }
    129.         }
    130.      
    131.     }
    132.  
    133. }
     
    Last edited: Mar 11, 2020
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Don't make us count to line 74 mentioned in the error. Just say which line specifically the error is referring to. Also, post code using CODE tags. See the sticky at the top of the scripting forum.
     
  3. RazaTech

    RazaTech

    Joined:
    Feb 27, 2015
    Posts:
    178
    its solved ?
     
  4. Akash1919

    Akash1919

    Joined:
    Apr 6, 2020
    Posts:
    1
    did you get any solution ??
    i am having same error in my current project
    please reply..