/// <summary>
        /// Websocket transport specific connection logic. It will create a WebSocket instance, and starts to connect to the server.
        /// </summary>
        public override void Connect()
        {
            if (wSocket != null)
            {
                HTTPManager.Logger.Warning("WebSocketTransport", "Start - WebSocket already created!");
                return;
            }

            // Skip the Connecting state if we are reconnecting. If the connect succeeds, we will set the Started state directly
            if (this.State != TransportStates.Reconnecting)
                this.State = TransportStates.Connecting;

            RequestTypes requestType = this.State == TransportStates.Reconnecting ? RequestTypes.Reconnect : RequestTypes.Connect;

            Uri uri = Connection.BuildUri(requestType, this);

            // Create the WebSocket instance
            wSocket = new WebSocket.WebSocket(uri);

            // Set up eventhandlers
            wSocket.OnOpen += WSocket_OnOpen;
            wSocket.OnMessage += WSocket_OnMessage;
            wSocket.OnClosed += WSocket_OnClosed;
            wSocket.OnErrorDesc += WSocket_OnError;

            // prepare the internal http request
            Connection.PrepareRequest(wSocket.InternalRequest, requestType);

            // start opening the websocket protocol
            wSocket.Open();
        }
Ejemplo n.º 2
0
    static int Close(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 1 && TypeChecker.CheckTypes(L, 1, typeof(BestHTTP.WebSocket.WebSocket)))
            {
                BestHTTP.WebSocket.WebSocket obj = (BestHTTP.WebSocket.WebSocket)ToLua.ToObject(L, 1);
                obj.Close();
                return(0);
            }
            else if (count == 3 && TypeChecker.CheckTypes(L, 1, typeof(BestHTTP.WebSocket.WebSocket), typeof(ushort), typeof(string)))
            {
                BestHTTP.WebSocket.WebSocket obj = (BestHTTP.WebSocket.WebSocket)ToLua.ToObject(L, 1);
                ushort arg0 = (ushort)LuaDLL.lua_tonumber(L, 2);
                string arg1 = ToLua.ToString(L, 3);
                obj.Close(arg0, arg1);
                return(0);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: BestHTTP.WebSocket.WebSocket.Close"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
Ejemplo n.º 3
0
 private void OnBinary(BestHTTP.WebSocket.WebSocket ws, byte[] data)
 {
     if (ws == this.Implementation)
     {
         if (HTTPManager.Logger.Level <= Loglevels.All)
         {
             HTTPManager.Logger.Verbose("WebSocketTransport", "OnBinary");
         }
         if (this.PacketWithAttachment != null)
         {
             this.PacketWithAttachment.AddAttachmentFromServer(data, false);
             if (this.PacketWithAttachment.HasAllAttachment)
             {
                 try
                 {
                     this.OnPacket(this.PacketWithAttachment);
                 }
                 catch (Exception exception)
                 {
                     HTTPManager.Logger.Exception("WebSocketTransport", "OnBinary", exception);
                 }
                 finally
                 {
                     this.PacketWithAttachment = null;
                 }
             }
         }
     }
 }
        public void Open()
        {
            if (State != TransportStates.Closed)
                return;

            Uri uri = new Uri(string.Format("{0}?transport=websocket&sid={1}{2}",
                                             new UriBuilder(HTTPProtocolFactory.IsSecureProtocol(Manager.Uri) ? "wss" : "ws", 
                                                            Manager.Uri.Host, 
                                                            Manager.Uri.Port, 
                                                            Manager.Uri.PathAndQuery).Uri.ToString(),
                                             Manager.Handshake.Sid,
                                             !Manager.Options.QueryParamsOnlyForHandshake ? Manager.Options.BuildQueryParams() : string.Empty));

            Implementation = new WebSocket(uri);

            Implementation.OnOpen = OnOpen;
            Implementation.OnMessage = OnMessage;
            Implementation.OnBinary = OnBinary;
            Implementation.OnError = OnError;
            Implementation.OnClosed = OnClosed;

            Implementation.Open();

            State = TransportStates.Connecting;
        }
Ejemplo n.º 5
0
    static int _CreateBestHTTP_WebSocket_WebSocket(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 1)
            {
                System.Uri arg0 = (System.Uri)ToLua.CheckObject(L, 1, typeof(System.Uri));
                BestHTTP.WebSocket.WebSocket obj = new BestHTTP.WebSocket.WebSocket(arg0);
                ToLua.PushObject(L, obj);
                return(1);
            }
            else if (TypeChecker.CheckTypes(L, 1, typeof(System.Uri), typeof(string), typeof(string)) && TypeChecker.CheckParamsType(L, typeof(BestHTTP.WebSocket.Extensions.IExtension), 4, count - 3))
            {
                System.Uri arg0 = (System.Uri)ToLua.CheckObject(L, 1, typeof(System.Uri));
                string     arg1 = ToLua.CheckString(L, 2);
                string     arg2 = ToLua.CheckString(L, 3);
                BestHTTP.WebSocket.Extensions.IExtension[] arg3 = ToLua.CheckParamsObject <BestHTTP.WebSocket.Extensions.IExtension>(L, 4, count - 3);
                BestHTTP.WebSocket.WebSocket obj = new BestHTTP.WebSocket.WebSocket(arg0, arg1, arg2, arg3);
                ToLua.PushObject(L, obj);
                return(1);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to ctor method: BestHTTP.WebSocket.WebSocket.New"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
Ejemplo n.º 6
0
 private void OnMessage(BestHTTP.WebSocket.WebSocket ws, string message)
 {
     if (ws == this.Implementation)
     {
         if (HTTPManager.Logger.Level <= Loglevels.All)
         {
             HTTPManager.Logger.Verbose("WebSocketTransport", "OnMessage: " + message);
         }
         try
         {
             Packet packet = new Packet(message);
             if (packet.AttachmentCount == 0)
             {
                 this.OnPacket(packet);
             }
             else
             {
                 this.PacketWithAttachment = packet;
             }
         }
         catch (Exception exception)
         {
             HTTPManager.Logger.Exception("WebSocketTransport", "OnMessage", exception);
         }
     }
 }
Ejemplo n.º 7
0
    static int set_OnIncompleteFrame(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            BestHTTP.WebSocket.WebSocket obj = (BestHTTP.WebSocket.WebSocket)o;
            BestHTTP.WebSocket.OnWebSocketIncompleteFrameDelegate arg0 = null;
            LuaTypes funcType2 = LuaDLL.lua_type(L, 2);

            if (funcType2 != LuaTypes.LUA_TFUNCTION)
            {
                arg0 = (BestHTTP.WebSocket.OnWebSocketIncompleteFrameDelegate)ToLua.CheckObject(L, 2, typeof(BestHTTP.WebSocket.OnWebSocketIncompleteFrameDelegate));
            }
            else
            {
                LuaFunction func = ToLua.ToLuaFunction(L, 2);
                arg0 = DelegateFactory.CreateDelegate(typeof(BestHTTP.WebSocket.OnWebSocketIncompleteFrameDelegate), func) as BestHTTP.WebSocket.OnWebSocketIncompleteFrameDelegate;
            }

            obj.OnIncompleteFrame = arg0;
            return(0);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index OnIncompleteFrame on a nil value" : e.Message));
        }
    }
Ejemplo n.º 8
0
 protected override void Aborted()
 {
     if ((this.wSocket != null) && this.wSocket.IsOpen)
     {
         this.wSocket.Close();
         this.wSocket = null;
     }
 }
Ejemplo n.º 9
0
 private void WSocket_OnOpen(BestHTTP.WebSocket.WebSocket webSocket)
 {
     if (webSocket == this.wSocket)
     {
         HTTPManager.Logger.Information("WebSocketTransport", "WSocket_OnOpen");
         base.OnConnected();
     }
 }
        /// <summary>
        /// WebSocket implementation OnOpen event handler.
        /// </summary>
        private void OnOpen(WebSocket ws)
        {
            HTTPManager.Logger.Information("WebSocketTransport", "OnOpen");

            State = TransportStates.Opening;

            // Send Ping Probe
            Send(new Packet(TransportEventTypes.Ping, SocketIOEventTypes.Unknown, "/", "probe"));
        }
Ejemplo n.º 11
0
    private void OnError(BestHTTP.WebSocket.WebSocket ws, Exception ex)
    {
        string str = string.Empty;

        if (ws.InternalRequest.Response != null)
        {
            str = $"Status Code from Server: {ws.InternalRequest.Response.StatusCode} and Message: {ws.InternalRequest.Response.Message}";
        }
        Debugger.Log("OnError " + str);
    }
        /// <summary>
        /// Closes the transport and cleans up resources.
        /// </summary>
        public void Close()
        {
            if (State == TransportStates.Closed)
                return;

            State = TransportStates.Closed;

            Implementation.Close();
            Implementation = null;
        }
Ejemplo n.º 13
0
 private void WSocket_OnMessage(BestHTTP.WebSocket.WebSocket webSocket, string message)
 {
     if (webSocket == this.wSocket)
     {
         IServerMessage msg = TransportBase.Parse(base.Connection.JsonEncoder, message);
         if (msg != null)
         {
             base.Connection.OnMessage(msg);
         }
     }
 }
 public override void Stop()
 {
     if (wSocket != null && wSocket.IsOpen)
     {
         wSocket.OnOpen = null;
         wSocket.OnMessage = null;
         wSocket.OnClosed = null;
         wSocket.OnErrorDesc = null;
         wSocket.Close();
         wSocket = null;
     }
 }
Ejemplo n.º 15
0
 public override void Stop()
 {
     if (this.wSocket != null)
     {
         this.wSocket.OnOpen      = null;
         this.wSocket.OnMessage   = null;
         this.wSocket.OnClosed    = null;
         this.wSocket.OnErrorDesc = null;
         this.wSocket.Close();
         this.wSocket = null;
     }
 }
Ejemplo n.º 16
0
    private void OnMessageReceived(BestHTTP.WebSocket.WebSocket ws, string message)
    {
        try
        {
            MatchMessage message2 = JsonConvert.DeserializeObject <MatchMessage>(message);
            if (!string.IsNullOrEmpty(message2.userid))
            {
                if (string.IsNullOrEmpty(this.otheruserid))
                {
                    this.otheruserid = message2.userid;
                }
                else if (this.otheruserid != message2.userid)
                {
                    return;
                }
            }
            switch (((MatchMessageType)message2.msgtype))
            {
            case MatchMessageType.eLearnSkill:
                GameLogic.Hold.BattleData.Challenge_SendEvent("MatchDefenceTime_other_learn_skill", message2.argint);
                return;

            case MatchMessageType.eDead:
                GameLogic.Hold.BattleData.Challenge_SendEvent("MatchDefenceTime_other_dead", null);
                return;

            case MatchMessageType.eReborn:
                GameLogic.Hold.BattleData.Challenge_SendEvent("MatchDefenceTime_other_reborn", null);
                return;

            case MatchMessageType.eScoreUpdate:
                GameLogic.Hold.BattleData.Challenge_SendEvent("MatchDefenceTime_other_updatescore", message2.argint);
                return;

            case MatchMessageType.eGameStart:
            {
                long argint = message2.argint;
                GameLogic.Hold.BattleData.Challenge_UpdateMode(0x332d, BattleSource.eMatch);
                GameLogic.Hold.BattleData.Challenge_SendEvent("MatchDefenceTime_set_random_seed", argint);
                WindowUI.ShowWindow(WindowID.WindowID_Battle);
                GameLogic.Hold.BattleData.Challenge_SendEvent("MatchDefenceTime_me_updatename", this.myname);
                GameLogic.Hold.BattleData.Challenge_SendEvent("MatchDefenceTime_other_updatename", message2.nickname);
                return;
            }

            case MatchMessageType.eGameEnd:
                return;
            }
        }
        catch
        {
        }
    }
Ejemplo n.º 17
0
 private void OnOpen(BestHTTP.WebSocket.WebSocket ws)
 {
     if (ws == this.Implementation)
     {
         HTTPManager.Logger.Information("WebSocketTransport", "OnOpen");
         this.State = TransportStates.Opening;
         if (this.Manager.UpgradingTransport == this)
         {
             this.Send(new Packet(TransportEventTypes.Ping, SocketIOEventTypes.Unknown, "/", "probe", 0, 0));
         }
     }
 }
Ejemplo n.º 18
0
 public void init()
 {
     if (this.webSocket == null)
     {
         this.webSocket           = new BestHTTP.WebSocket.WebSocket(new Uri("wss://api-archer.habby.mobi:4443/matching"));
         this.webSocket.OnOpen    = (OnWebSocketOpenDelegate)Delegate.Combine(this.webSocket.OnOpen, new OnWebSocketOpenDelegate(this.OnOpen));
         this.webSocket.OnMessage = (OnWebSocketMessageDelegate)Delegate.Combine(this.webSocket.OnMessage, new OnWebSocketMessageDelegate(this.OnMessageReceived));
         this.webSocket.OnBinary  = (OnWebSocketBinaryDelegate)Delegate.Combine(this.webSocket.OnBinary, new OnWebSocketBinaryDelegate(this.OnBinaryReceived));
         this.webSocket.OnError   = (OnWebSocketErrorDelegate)Delegate.Combine(this.webSocket.OnError, new OnWebSocketErrorDelegate(this.OnError));
         this.webSocket.OnClosed  = (OnWebSocketClosedDelegate)Delegate.Combine(this.webSocket.OnClosed, new OnWebSocketClosedDelegate(this.OnClosed));
     }
 }
Ejemplo n.º 19
0
    private void OnError(BestHTTP.WebSocket.WebSocket ws, Exception ex)
    {
        string str = string.Empty;

        if (ws.InternalRequest.Response != null)
        {
            str = $"Status Code from Server: {ws.InternalRequest.Response.StatusCode} and Message: {ws.InternalRequest.Response.Message}";
        }
        this.Text      = this.Text + $"-An error occured: {((ex == null) ? ("Unknown Error " + str) : ex.Message)}
";
        this.webSocket = null;
    }
        /// <summary>
        /// Closes the transport and cleans up resources.
        /// </summary>
        public void Close()
        {
            if (State == TransportStates.Closed)
                return;

            State = TransportStates.Closed;

            if (Implementation != null)
                Implementation.Close();
            else
                HTTPManager.Logger.Warning("WebSocketTransport", "Close - WebSocket Implementation already null!");
            Implementation = null;
        }
Ejemplo n.º 21
0
 public void Close()
 {
     if (this.webSocket != null)
     {
         this.Send(MatchMessageType.eUserLeave, 0);
         this.webSocket.OnOpen    = null;
         this.webSocket.OnMessage = null;
         this.webSocket.OnError   = null;
         this.webSocket.OnClosed  = null;
         this.webSocket.Close();
         this.webSocket = null;
         this.mState    = ConnectState.eClose;
     }
 }
Ejemplo n.º 22
0
 static int Open(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 1);
         BestHTTP.WebSocket.WebSocket obj = (BestHTTP.WebSocket.WebSocket)ToLua.CheckObject(L, 1, typeof(BestHTTP.WebSocket.WebSocket));
         obj.Open();
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Ejemplo n.º 23
0
        private void OnError(BestHTTP.WebSocket.WebSocket ws, Exception ex)
        {
            if (ws == this.Implementation)
            {
                string err = string.Empty;
                if (ex != null)
                {
                    err = ex.Message + " " + ex.StackTrace;
                }
                else
                {
                    switch (ws.InternalRequest.State)
                    {
                    case HTTPRequestStates.Finished:
                        if (!ws.InternalRequest.Response.IsSuccess && (ws.InternalRequest.Response.StatusCode != 0x65))
                        {
                            err = $"Request Finished Successfully, but the server sent an error. Status Code: {ws.InternalRequest.Response.StatusCode}-{ws.InternalRequest.Response.Message} Message: {ws.InternalRequest.Response.DataAsText}";
                            break;
                        }
                        err = $"Request finished. Status Code: {ws.InternalRequest.Response.StatusCode.ToString()} Message: {ws.InternalRequest.Response.Message}";
                        break;

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

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

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

                    case HTTPRequestStates.TimedOut:
                        err = "Processing the request Timed Out!";
                        break;
                    }
                }
                if (this.Manager.UpgradingTransport != this)
                {
                    ((IManager)this.Manager).OnTransportError(this, err);
                }
                else
                {
                    this.Manager.UpgradingTransport = null;
                }
            }
        }
Ejemplo n.º 24
0
 private void WSocket_OnError(BestHTTP.WebSocket.WebSocket webSocket, string reason)
 {
     if (webSocket == this.wSocket)
     {
         if ((base.State == TransportStates.Closing) || (base.State == TransportStates.Closed))
         {
             base.AbortFinished();
         }
         else
         {
             HTTPManager.Logger.Error("WebSocketTransport", "WSocket_OnError " + reason);
             base.Connection.Error(reason);
         }
     }
 }
Ejemplo n.º 25
0
 private void WSocket_OnClosed(BestHTTP.WebSocket.WebSocket webSocket, ushort code, string message)
 {
     if (webSocket == this.wSocket)
     {
         string reason = code.ToString() + " : " + message;
         HTTPManager.Logger.Information("WebSocketTransport", "WSocket_OnClosed " + reason);
         if (base.State == TransportStates.Closing)
         {
             base.State = TransportStates.Closed;
         }
         else
         {
             base.Connection.Error(reason);
         }
     }
 }
Ejemplo n.º 26
0
 private void OnClosed(BestHTTP.WebSocket.WebSocket ws, ushort code, string message)
 {
     if (ws == this.Implementation)
     {
         HTTPManager.Logger.Information("WebSocketTransport", "OnClosed");
         this.Close();
         if (this.Manager.UpgradingTransport != this)
         {
             ((IManager)this.Manager).TryToReconnect();
         }
         else
         {
             this.Manager.UpgradingTransport = null;
         }
     }
 }
Ejemplo n.º 27
0
    static int get_Extensions(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            BestHTTP.WebSocket.WebSocket obj = (BestHTTP.WebSocket.WebSocket)o;
            BestHTTP.WebSocket.Extensions.IExtension[] ret = obj.Extensions;
            ToLua.Push(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index Extensions on a nil value" : e.Message));
        }
    }
Ejemplo n.º 28
0
    static int get_InternalRequest(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            BestHTTP.WebSocket.WebSocket obj = (BestHTTP.WebSocket.WebSocket)o;
            BestHTTP.HTTPRequest         ret = obj.InternalRequest;
            ToLua.Push(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index InternalRequest on a nil value" : e.Message));
        }
    }
Ejemplo n.º 29
0
    static int get_PingFrequency(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            BestHTTP.WebSocket.WebSocket obj = (BestHTTP.WebSocket.WebSocket)o;
            int ret = obj.PingFrequency;
            LuaDLL.lua_pushinteger(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index PingFrequency on a nil value" : e.Message));
        }
    }
Ejemplo n.º 30
0
    static int get_OnIncompleteFrame(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            BestHTTP.WebSocket.WebSocket obj = (BestHTTP.WebSocket.WebSocket)o;
            BestHTTP.WebSocket.OnWebSocketIncompleteFrameDelegate ret = obj.OnIncompleteFrame;
            ToLua.Push(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index OnIncompleteFrame on a nil value" : e.Message));
        }
    }
Ejemplo n.º 31
0
    static int get_webSocket(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            GameSocket obj = (GameSocket)o;
            BestHTTP.WebSocket.WebSocket ret = obj.webSocket;
            ToLua.PushSealed(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o, "attempt to index webSocket on a nil value"));
        }
    }
Ejemplo n.º 32
0
    static int set_webSocket(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            GameSocket obj = (GameSocket)o;
            BestHTTP.WebSocket.WebSocket arg0 = (BestHTTP.WebSocket.WebSocket)ToLua.CheckObject(L, 2, typeof(BestHTTP.WebSocket.WebSocket));
            obj.webSocket = arg0;
            return(0);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o, "attempt to index webSocket on a nil value"));
        }
    }
Ejemplo n.º 33
0
    static int set_StartPingThread(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            BestHTTP.WebSocket.WebSocket obj = (BestHTTP.WebSocket.WebSocket)o;
            bool arg0 = LuaDLL.luaL_checkboolean(L, 2);
            obj.StartPingThread = arg0;
            return(0);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index StartPingThread on a nil value" : e.Message));
        }
    }
Ejemplo n.º 34
0
    static int get_StartPingThread(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            BestHTTP.WebSocket.WebSocket obj = (BestHTTP.WebSocket.WebSocket)o;
            bool ret = obj.StartPingThread;
            LuaDLL.lua_pushboolean(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index StartPingThread on a nil value" : e.Message));
        }
    }
Ejemplo n.º 35
0
    static int Send(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 2 && TypeChecker.CheckTypes(L, 1, typeof(BestHTTP.WebSocket.WebSocket), typeof(BestHTTP.WebSocket.Frames.WebSocketFrame)))
            {
                BestHTTP.WebSocket.WebSocket             obj  = (BestHTTP.WebSocket.WebSocket)ToLua.ToObject(L, 1);
                BestHTTP.WebSocket.Frames.WebSocketFrame arg0 = (BestHTTP.WebSocket.Frames.WebSocketFrame)ToLua.ToObject(L, 2);
                obj.Send(arg0);
                return(0);
            }
            else if (count == 2 && TypeChecker.CheckTypes(L, 1, typeof(BestHTTP.WebSocket.WebSocket), typeof(byte[])))
            {
                BestHTTP.WebSocket.WebSocket obj = (BestHTTP.WebSocket.WebSocket)ToLua.ToObject(L, 1);
                byte[] arg0 = ToLua.CheckByteBuffer(L, 2);
                obj.Send(arg0);
                return(0);
            }
            else if (count == 2 && TypeChecker.CheckTypes(L, 1, typeof(BestHTTP.WebSocket.WebSocket), typeof(string)))
            {
                BestHTTP.WebSocket.WebSocket obj = (BestHTTP.WebSocket.WebSocket)ToLua.ToObject(L, 1);
                string arg0 = ToLua.ToString(L, 2);
                obj.Send(arg0);
                return(0);
            }
            else if (count == 4 && TypeChecker.CheckTypes(L, 1, typeof(BestHTTP.WebSocket.WebSocket), typeof(byte[]), typeof(ulong), typeof(ulong)))
            {
                BestHTTP.WebSocket.WebSocket obj = (BestHTTP.WebSocket.WebSocket)ToLua.ToObject(L, 1);
                byte[] arg0 = ToLua.CheckByteBuffer(L, 2);
                ulong  arg1 = (ulong)LuaDLL.lua_tonumber(L, 3);
                ulong  arg2 = (ulong)LuaDLL.lua_tonumber(L, 4);
                obj.Send(arg0, arg1, arg2);
                return(0);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: BestHTTP.WebSocket.WebSocket.Send"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
Ejemplo n.º 36
0
    static int set_PingFrequency(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            BestHTTP.WebSocket.WebSocket obj = (BestHTTP.WebSocket.WebSocket)o;
            int arg0 = (int)LuaDLL.luaL_checknumber(L, 2);
            obj.PingFrequency = arg0;
            return(0);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index PingFrequency on a nil value" : e.Message));
        }
    }
        /// <summary>
        /// WebSocket implementation OnClosed event handler.
        /// </summary>
        private void OnClosed(WebSocket ws, ushort code, string message)
        {
            HTTPManager.Logger.Information("WebSocketTransport", "OnClosed");

            Close();

            (Manager as IManager).TryToReconnect();
        }
        /// <summary>
        /// WebSocket implementation OnBinary event handler.
        /// </summary>
        private void OnBinary(WebSocket ws, byte[] data)
        {
            if (HTTPManager.Logger.Level <= BestHTTP.Logger.Loglevels.All)
                HTTPManager.Logger.Verbose("WebSocketTransport", "OnBinary");

            if (PacketWithAttachment != null)
            {
                PacketWithAttachment.AddAttachmentFromServer(data, false);

                if (PacketWithAttachment.HasAllAttachment)
                {
                    try
                    {
                        OnPacket(PacketWithAttachment);
                    }
                    catch (Exception ex)
                    {
                        HTTPManager.Logger.Exception("WebSocketTransport", "OnBinary", ex);
                    }
                    finally
                    {
                        PacketWithAttachment = null;
                    }
                }
            }
            else
            {
                // TODO: we received an unwanted binary message?
            }
        }
        /// <summary>
        /// WebSocket implementation OnMessage event handler.
        /// </summary>
        private void OnMessage(WebSocket ws, string message)
        {
            if (HTTPManager.Logger.Level <= BestHTTP.Logger.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);
            }
        }
 /// <summary>
 /// The /abort request successfully finished
 /// </summary>
 protected override void Aborted()
 {
     // if the websocket is still open, close it
     if (wSocket != null && wSocket.IsOpen)
     {
         wSocket.Close();
         wSocket = null;
     }
 }
        /// <summary>
        /// WebSocket implementation OnError event handler.
        /// </summary>
        private void OnError(WebSocket ws, Exception ex)
        {
            string errorStr = string.Empty;

            if (ex != null)
                errorStr = (ex.Message + " " + ex.StackTrace);
            else
            {
                switch (ws.InternalRequest.State)
                {
                    // The request finished without any problem.
                    case HTTPRequestStates.Finished:
                        if (ws.InternalRequest.Response.IsSuccess || ws.InternalRequest.Response.StatusCode == 101)
                            errorStr = string.Format("Request finished. Status Code: {0} Message: {1}", ws.InternalRequest.Response.StatusCode.ToString(), ws.InternalRequest.Response.Message);
                        else
                            errorStr = string.Format("Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
                                                            ws.InternalRequest.Response.StatusCode,
                                                            ws.InternalRequest.Response.Message,
                                                            ws.InternalRequest.Response.DataAsText);
                        break;

                    // The request finished with an unexpected error. The request's Exception property may contain more info about the error.
                    case HTTPRequestStates.Error:
                        errorStr = "Request Finished with Error! : " + ws.InternalRequest.Exception != null ? (ws.InternalRequest.Exception.Message + " " + ws.InternalRequest.Exception.StackTrace) : string.Empty;
                        break;

                    // The request aborted, initiated by the user.
                    case HTTPRequestStates.Aborted:
                        errorStr = "Request Aborted!";
                        break;

                    // Ceonnecting to the server is timed out.
                    case HTTPRequestStates.ConnectionTimedOut:
                        errorStr = "Connection Timed Out!";
                        break;

                    // The request didn't finished in the given time.
                    case HTTPRequestStates.TimedOut:
                        errorStr = "Processing the request Timed Out!";
                        break;
                }
            }

            HTTPManager.Logger.Error("WebSocketTransport", "OnError: " + errorStr);

            (Manager as IManager).OnTransportError(this, errorStr);
        }