第一次做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, Func
2 endFunction, Action1 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.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func
2 endFunction, Action1 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.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func
2 endFunction, Action1 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.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func
2 endFunction, Action1 endAction, Task
1 promise, Boolean requiresSynchronization)
有个监听出错自动重启,在找到具体原因之前可以暂时使用。
具体哪个监听函数呢
server.RestartAfterListenError = true;
是这个吧
@dgtg77: 是的,看看有作用没有,虽然只是治标的方法。