永动bug制造机 2021-08-23 13:46 采纳率: 0%
浏览 56

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

第一次做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.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 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.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 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.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 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.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)<---
  • 写回答

1条回答 默认 最新

  • larwar 2021-08-24 19:28
    关注

    传输流是0个字节

    评论

    报告相同问题?

    问题事件

    • 创建了问题 8月23日

    悬赏问题

    • ¥15 关于网上一个easyx制作的见缝插针小游戏(c++)
    • ¥15 开地址法双散列函数处理碰撞
    • ¥15 想问一下这个是什么情况 虚拟机Linux打不开了
    • ¥15 联通光猫掉注册了怎么重新注册上去
    • ¥15 关于unity开发steamvr程序遇到的问题
    • ¥60 求tc downloader的下载方式
    • ¥15 华为 快捷方式 手电筒 接口
    • ¥15 Qt6.5支不支持Android13开发啊
    • ¥20 网络只能跑一半,应该如何设置
    • ¥20 Python调用百度开发者平台人脸识别接口