Example #1
0
 public override void Connect()
 {
     if (wSocket != null)
     {
         HTTPManager.Logger.Warning("WebSocketTransport", "Start - WebSocket already created!");
     }
     else
     {
         if (base.State != TransportStates.Reconnecting)
         {
             base.State = TransportStates.Connecting;
         }
         RequestTypes type = (base.State != TransportStates.Reconnecting) ? RequestTypes.Connect : RequestTypes.Reconnect;
         Uri          uri  = base.Connection.BuildUri(type, this);
         wSocket = new VRC.Core.BestHTTP.WebSocket.WebSocket(uri);
         VRC.Core.BestHTTP.WebSocket.WebSocket webSocket = wSocket;
         webSocket.OnOpen = (OnWebSocketOpenDelegate)Delegate.Combine(webSocket.OnOpen, new OnWebSocketOpenDelegate(WSocket_OnOpen));
         VRC.Core.BestHTTP.WebSocket.WebSocket webSocket2 = wSocket;
         webSocket2.OnMessage = (OnWebSocketMessageDelegate)Delegate.Combine(webSocket2.OnMessage, new OnWebSocketMessageDelegate(WSocket_OnMessage));
         VRC.Core.BestHTTP.WebSocket.WebSocket webSocket3 = wSocket;
         webSocket3.OnClosed = (OnWebSocketClosedDelegate)Delegate.Combine(webSocket3.OnClosed, new OnWebSocketClosedDelegate(WSocket_OnClosed));
         VRC.Core.BestHTTP.WebSocket.WebSocket webSocket4 = wSocket;
         webSocket4.OnErrorDesc = (OnWebSocketErrorDescriptionDelegate)Delegate.Combine(webSocket4.OnErrorDesc, new OnWebSocketErrorDescriptionDelegate(WSocket_OnError));
         base.Connection.PrepareRequest(wSocket.InternalRequest, type);
         wSocket.Open();
     }
 }
 private void OnBinary(VRC.Core.BestHTTP.WebSocket.WebSocket ws, byte[] data)
 {
     if (ws == Implementation)
     {
         if (HTTPManager.Logger.Level <= Loglevels.All)
         {
             HTTPManager.Logger.Verbose("WebSocketTransport", "OnBinary");
         }
         if (PacketWithAttachment != null)
         {
             PacketWithAttachment.AddAttachmentFromServer(data, copyFull: false);
             if (PacketWithAttachment.HasAllAttachment)
             {
                 try
                 {
                     OnPacket(PacketWithAttachment);
                 }
                 catch (Exception ex)
                 {
                     HTTPManager.Logger.Exception("WebSocketTransport", "OnBinary", ex);
                 }
                 finally
                 {
                     PacketWithAttachment = null;
                 }
             }
         }
     }
 }
 private void OnMessage(VRC.Core.BestHTTP.WebSocket.WebSocket ws, string message)
 {
     if (ws == Implementation)
     {
         if (HTTPManager.Logger.Level <= Loglevels.All)
         {
             HTTPManager.Logger.Verbose("WebSocketTransport", "OnMessage: " + message);
         }
         try
         {
             Packet packet = new Packet(message);
             if (packet.AttachmentCount == 0)
             {
                 OnPacket(packet);
             }
             else
             {
                 PacketWithAttachment = packet;
             }
         }
         catch (Exception ex)
         {
             HTTPManager.Logger.Exception("WebSocketTransport", "OnMessage", ex);
         }
     }
 }
Example #4
0
 private void WSocket_OnOpen(VRC.Core.BestHTTP.WebSocket.WebSocket webSocket)
 {
     if (webSocket == wSocket)
     {
         HTTPManager.Logger.Information("WebSocketTransport", "WSocket_OnOpen");
         OnConnected();
     }
 }
Example #5
0
 protected override void Aborted()
 {
     if (wSocket != null && wSocket.IsOpen)
     {
         wSocket.Close();
         wSocket = null;
     }
 }
 private void OnOpen(VRC.Core.BestHTTP.WebSocket.WebSocket ws)
 {
     if (ws == Implementation)
     {
         HTTPManager.Logger.Information("WebSocketTransport", "OnOpen");
         State = TransportStates.Opening;
         Send(new Packet(TransportEventTypes.Ping, SocketIOEventTypes.Unknown, "/", "probe"));
     }
 }
 private void OnClosed(VRC.Core.BestHTTP.WebSocket.WebSocket ws, ushort code, string message)
 {
     if (ws == Implementation)
     {
         HTTPManager.Logger.Information("WebSocketTransport", "OnClosed");
         Close();
         ((IManager)Manager).TryToReconnect();
     }
 }
Example #8
0
 private void WSocket_OnMessage(VRC.Core.BestHTTP.WebSocket.WebSocket webSocket, string message)
 {
     if (webSocket == wSocket)
     {
         IServerMessage serverMessage = TransportBase.Parse(base.Connection.JsonEncoder, message);
         if (serverMessage != null)
         {
             base.Connection.OnMessage(serverMessage);
         }
     }
 }
Example #9
0
 public override void Stop()
 {
     if (wSocket != null)
     {
         wSocket.OnOpen      = null;
         wSocket.OnMessage   = null;
         wSocket.OnClosed    = null;
         wSocket.OnErrorDesc = null;
         wSocket.Close();
         wSocket = null;
     }
 }
Example #10
0
 private void WSocket_OnError(VRC.Core.BestHTTP.WebSocket.WebSocket webSocket, string reason)
 {
     if (webSocket == wSocket)
     {
         if (base.State == TransportStates.Closing || base.State == TransportStates.Closed)
         {
             AbortFinished();
         }
         else
         {
             HTTPManager.Logger.Error("WebSocketTransport", "WSocket_OnError " + reason);
             base.Connection.Error(reason);
         }
     }
 }
 public void Open()
 {
     if (State == TransportStates.Closed)
     {
         Uri uri = new Uri(string.Format("{0}?transport=websocket&sid={1}{2}", new UriBuilder((!HTTPProtocolFactory.IsSecureProtocol(Manager.Uri)) ? "ws" : "wss", Manager.Uri.Host, Manager.Uri.Port, Manager.Uri.PathAndQuery).Uri.ToString(), Manager.Handshake.Sid, Manager.Options.QueryParamsOnlyForHandshake ? string.Empty : Manager.Options.BuildQueryParams()));
         Implementation           = new VRC.Core.BestHTTP.WebSocket.WebSocket(uri);
         Implementation.OnOpen    = OnOpen;
         Implementation.OnMessage = OnMessage;
         Implementation.OnBinary  = OnBinary;
         Implementation.OnError   = OnError;
         Implementation.OnClosed  = OnClosed;
         Implementation.Open();
         State = TransportStates.Connecting;
     }
 }
Example #12
0
 private void WSocket_OnClosed(VRC.Core.BestHTTP.WebSocket.WebSocket webSocket, ushort code, string message)
 {
     if (webSocket == wSocket)
     {
         string text = code.ToString() + " : " + message;
         HTTPManager.Logger.Information("WebSocketTransport", "WSocket_OnClosed " + text);
         if (base.State == TransportStates.Closing)
         {
             base.State = TransportStates.Closed;
         }
         else
         {
             base.Connection.Error(text);
         }
     }
 }
 public void Close()
 {
     if (State != TransportStates.Closed)
     {
         State = TransportStates.Closed;
         if (Implementation != null)
         {
             Implementation.Close();
         }
         else
         {
             HTTPManager.Logger.Warning("WebSocketTransport", "Close - WebSocket Implementation already null!");
         }
         Implementation = null;
     }
 }
        private void OnError(VRC.Core.BestHTTP.WebSocket.WebSocket ws, Exception ex)
        {
            if (ws == Implementation)
            {
                string text = string.Empty;
                if (ex == null)
                {
                    switch (ws.InternalRequest.State)
                    {
                    case HTTPRequestStates.Finished:
                        text = ((!ws.InternalRequest.Response.IsSuccess && ws.InternalRequest.Response.StatusCode != 101) ? $"Request Finished Successfully, but the server sent an error. Status Code: {ws.InternalRequest.Response.StatusCode}-{ws.InternalRequest.Response.Message} Message: {ws.InternalRequest.Response.DataAsText}" : $"Request finished. Status Code: {ws.InternalRequest.Response.StatusCode.ToString()} Message: {ws.InternalRequest.Response.Message}");
                        break;

                    case HTTPRequestStates.Error:
                        text = (("Request Finished with Error! : " + ws.InternalRequest.Exception == null) ? string.Empty : (ws.InternalRequest.Exception.Message + " " + ws.InternalRequest.Exception.StackTrace));
                        break;

                    case HTTPRequestStates.Aborted:
                        text = "Request Aborted!";
                        break;

                    case HTTPRequestStates.ConnectionTimedOut:
                        text = "Connection Timed Out!";
                        break;

                    case HTTPRequestStates.TimedOut:
                        text = "Processing the request Timed Out!";
                        break;
                    }
                }
                else
                {
                    text = ex.Message + " " + ex.StackTrace;
                }
                HTTPManager.Logger.Error("WebSocketTransport", "OnError: " + text);
                ((IManager)Manager).OnTransportError((ITransport)this, text);
            }
        }