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

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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建