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

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条回答 默认 最新

相关推荐 更多相似问题