首页 新闻 会员 周边

c# 控制台程序写的Fleck Socket服务端出错

0
悬赏园豆:30 [已解决问题] 解决于 2022-06-25 10:07

第一次做websocket方面的需求,从网上找了fleck能快捷搭个服务端。使用了控制台程序。
现在服务端经常报“意外的数据包格式,握手失败”和“从传输流收到意外的EOF或0个字节”。错误后其他客户端就连不上了,需要重新运行服务端
服务端代码和错误日志如下

private static List<IWebSocketConnection> clientList = new
List<IWebSocketConnection>();

    static void Main(string[] args)
    {

        FleckLog.Level = LogLevel.Debug;

        string serverUrl = ConfigurationManager.AppSettings["SocketServer"];
        var server = new WebSocketServer(serverUrl); 

        string certFullPath = Environment.CurrentDirectory + "\\file\\6033136_socket.pfx";
        server.Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(certFullPath, "1rGcanD8");

        try
        {
            SocketStart(server);
            Console.ReadLine();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.Source);
            Console.WriteLine(ex.StackTrace);
        }

        Console.ReadKey();
    }

    private static void SocketStart(WebSocketServer server)
    {
        server.Start(socket =>
        {
            socket.OnOpen = () =>
            {
                try
                {
                    Console.WriteLine("Connect New");
                    clientList.Add(socket);
                }
                catch (Exception ex)
                {
                    LogerExt.LogError(ex);
                }
            };
            socket.OnClose = () =>
            {
                try
                {
                    Console.WriteLine("Connect Close!");
                    clientList.Remove(socket);
                }
                catch (Exception ex)
                {
                    LogerExt.LogError(ex);
                }
            };
            socket.OnMessage = message =>
            {
                try
                {
                    Console.WriteLine("New Message" + message);
                    JObject msgData = JObject.Parse(message);
                    var msgType = msgData["messageType"].ToStringExt();
                    switch (msgType)
                    {
                        case "joinsale":
                            JObject data = JObject.Parse(msgData["content"].ToStringExt());
                            ApiResponse res = ProjectService.Instance().FlashSale().SaleRoundJoin(data);

                            if (res.Code == 100 || res.Code == 101)
                            {
                                //给每个成员发送
                                foreach (var s in clientList)
                                {
                                    //发给自己的
                                    if (s == socket)
                                    {
                                        res.Source = 1;
                                        s.Send(res.ToJson);
                                    }
                                    else //发给其他参与者
                                    {
                                        res.Source = 2;
                                        s.Send(res.ToJson);
                                    }
                                }
                            }
                            else
                            {
                                socket.Send(res.ToJson);
                            }


                            break;
                        case "heart":
                            socket.Send(message);
                            break;
                    }
                }
                catch (Exception ex)
                {
                    LogerExt.LogError(ex);
                }
            };
        });
    }

2021/8/23 13:07:22 [Warn] Failed to Authenticate System.AggregateException: 发生一个或多个错误。 ---> System.IO.IOException: 从传输流收到意外的 EOF 或 0 个字节。
在 System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
在 System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
在 System.Net.Security.SslStream.EndAuthenticateAsServer(IAsyncResult asyncResult)
在 System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)
--- 内部异常堆栈跟踪的结尾 ---
---> (内部异常 #0) System.IO.IOException: 从传输流收到意外的 EOF 或 0 个字节。
在 System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
在 System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
在 System.Net.Security.SslStream.EndAuthenticateAsServer(IAsyncResult asyncResult)
在 System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)<---

2021/8/23 13:08:09 [Debug] Client connected from 183.136.225.14:15304
2021/8/23 13:08:09 [Debug] Authenticating Secure Connection
2021/8/23 13:08:09 [Warn] Failed to Authenticate System.AggregateException: 发生一个或多个错误。 ---> System.IO.IOException: 由于意外的数据包格式,握手失败。
在 System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
在 System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
在 System.Net.Security.SslStream.EndAuthenticateAsServer(IAsyncResult asyncResult)
在 System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)
--- 内部异常堆栈跟踪的结尾 ---
---> (内部异常 #0) System.IO.IOException: 由于意外的数据包格式,握手失败。
在 System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
在 System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
在 System.Net.Security.SslStream.EndAuthenticateAsServer(IAsyncResult asyncResult)
在 System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)

dgtg77的主页 dgtg77 | 初学一级 | 园豆:145
提问于:2021-08-23 14:06
< >
分享
最佳答案
1

有个监听出错自动重启,在找到具体原因之前可以暂时使用。

收获园豆:30
清海扬波 | 小虾三级 |园豆:825 | 2021-08-24 09:18

具体哪个监听函数呢

dgtg77 | 园豆:145 (初学一级) | 2021-08-28 18:30

server.RestartAfterListenError = true;

是这个吧

dgtg77 | 园豆:145 (初学一级) | 2021-08-28 18:36

@dgtg77: 是的,看看有作用没有,虽然只是治标的方法。

清海扬波 | 园豆:825 (小虾三级) | 2021-08-30 08:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册