单个消息后套接字会话关闭

时间:2017-05-31 18:12:02

标签: c# sockets websocket spring-websocket

调试正在使用Simple Web Socket Client。

因此,我的套接字在端口X上激活,

我在客户端上成功获得“Hello new client”消息。

当我将第一个请求/数据发送到服务器时,服务器接收它并在控制台上打印。

发布此消息后,只要我发送另一个请求,就会触发会话关闭事件。

我检查了Session的TimeOut:60secs和KeepAliveTime:600secs,看起来很好。

下面是SocketServer的代码:

static void Main(string[] args)
    {

        Console.WriteLine("Press any key to start the server!");

        Console.ReadKey();
        Console.WriteLine();


        var bootstrap = BootstrapFactory.CreateBootstrap();


        if (!bootstrap.Initialize())
        {
            Console.WriteLine("Failed to initialize!");
            Console.ReadKey();
            return;
        }

        var result = bootstrap.Start();

        Console.WriteLine("Start result: {0}!", result);

        if (result == StartResult.Failed)
        {
            Console.WriteLine("Failed to start!");
            Console.ReadKey();
            return;
        }

        foreach (WebSocketServer appS in bootstrap.AppServers)
        {
            appS.NewDataReceived += new SessionHandler<WebSocketSession, byte[]>(appServer_NewDataReceived);
            appS.NewSessionConnected += new SessionHandler<WebSocketSession>(appServer_NewSessionConnected);
            appS.SessionClosed += new SessionHandler<WebSocketSession, CloseReason>(appServer_SessionClosed);
            appS.NewMessageReceived += new SessionHandler<WebSocketSession, string>(appServer_NewMessageReceived);     
        }

        Console.WriteLine("Press key 'q' to stop it!");

        Char input;
        while ((input = Console.ReadKey().KeyChar) != 'q')
        {
            Console.WriteLine();
            continue;
        }


        Console.WriteLine();

        //Stop the appServer
        bootstrap.Stop();

        Console.WriteLine();
        Console.WriteLine("The server was stopped!");
        Console.ReadKey();
    }

    private static void appServer_NewDataReceived(WebSocketSession session, byte[] message)
    {
        Console.WriteLine("Client said: " + message);
        //Send the received message back
        session.Send("Server responded back: " + message);
    }

    private static void appServer_NewSessionConnected(WebSocketSession session)
    {
        Console.WriteLine();
        Console.WriteLine("New session connected! Sessions counter: " );
        session.Send("Hello new client!");
    }

    private static void appServer_SessionClosed(WebSocketSession session, CloseReason value)
    {
        Console.WriteLine();
        Console.WriteLine("Client disconnected! Sessions counter: ");
    }

    private static void appServer_NewMessageReceived(WebSocketSession session, string message)
    {
        Console.WriteLine("Client said: " + message);
        //Send the received message back
        session.Send("Server responded back: " + message);
    }

关闭错误是“协议错误”

0 个答案:

没有答案