赞
踩
Fleck是一个在C#中的WebSocket服务器端的一个实现.。分支自Nugget项目, Fleck不需要任何的继承、容器亦或是需要增加的引用就可以使用。
下面是一个把客户端网页发来的消息回发到客户端的服务器程序。
var server = new WebSocketServer("ws://0.0.0.0:8181"); server.Start(socket => { socket.OnOpen = () => Console.WriteLine("Open!"); socket.OnClose = () => Console.WriteLine("Close!"); socket.OnMessage = message => socket.Send(message); });
Fleck支持多种现代的具有WebSocket功能版本的浏览器。
启用安全的网络连接需要如下两件事:
1. 使用“wss”代替原来的“ws”;
2. 将Fleck指定一个包含一个共钥和私钥的x509证书;
var server = new WebSocketServer("wss://0.0.0.0:8431"); server.Certificate = new X509Certificate2("MyCert.pfx"); server.Start(socket => { //...use as normal });
为了启用子协议协商,在 WebSocketServer.SupportedSubProtocols
属性上进行指定声明。 协商的子协议将在套接字的 ConnectionInfo.NegotiatedSubProtocol
上可用。如果在客户端请求(在用于WebSocket打开阶段握手的头字段①)上没有找到支持的子协议,则连接将被关闭。
①这个地方我也不是很懂,要是有大神懂欢迎指正!
var server = new WebSocketServer("ws://0.0.0.0:8181");
server.SupportedSubProtocols = new []{ "superchat", "chat" };
server.Start(socket =>
{
//socket.ConnectionInfo.NegotiatedSubProtocol is populated
});
Fleck可以讲日志注入Log4Net等的一些第三方的日志系统中。仅仅需要用目标的日志行为方法注入一下FleckLog.LogAction
属性即可使用。
ILog logger = LogManager.GetLogger(typeof(FleckLog)); FleckLog.LogAction = (level, message, ex) => { switch(level) { case LogLevel.Debug: logger.Debug(message, ex); break; case LogLevel.Error: logger.Error(message, ex); break; case LogLevel.Warn: logger.Warn(message, ex); break; default: logger.Info(message, ex); break; } };
将WebSocketConnection.ListenerSocket
中的 NoDelay设置为true。
var server = new WebSocketServer("ws://0.0.0.0:8181"); server.ListenerSocket.NoDelay = true; server.Start(socket => { //Child connections will not use Nagle's Algorithm });
var server = new WebSocketServer("ws://0.0.0.0:8181"); server.RestartAfterListenError = true; server.Start(socket => { //...use as normal });
Fleck is a WebSocket server implementation in C#. Branched from the Nugget project, Fleck requires no inheritance, container, or additional references.
The following is an example that will echo to a client.
var server = new WebSocketServer("ws://0.0.0.0:8181");
server.Start(socket =>
{
socket.OnOpen = () => Console.WriteLine("Open!");
socket.OnClose = () => Console.WriteLine("Close!");
socket.OnMessage = message => socket.Send(message);
});
Fleck supports several WebSocket versions of modern web browsers
Enabling secure connections requires two things: using the scheme wss
instead of ws
, and pointing Fleck to an x509 certificate containing a public and private key
var server = new WebSocketServer("wss://0.0.0.0:8431");
server.Certificate = new X509Certificate2("MyCert.pfx");
server.Start(socket =>
{
//...use as normal
});
To enable negotiation of subprotocols, specify the supported protocols on theWebSocketServer.SupportedSubProtocols
property. The negotiated subprotocol will be available on the socket's ConnectionInfo.NegotiatedSubProtocol
.
If no supported subprotocols are found on the client request (the Sec-WebSocket-Protocol header), the connection will be closed.
var server = new WebSocketServer("ws://0.0.0.0:8181");
server.SupportedSubProtocols = new []{ "superchat", "chat" };
server.Start(socket =>
{
//socket.ConnectionInfo.NegotiatedSubProtocol is populated
});
Fleck can log into Log4Net or any other third party logging system. Just override the FleckLog.LogAction
property with the desired behavior.
ILog logger = LogManager.GetLogger(typeof(FleckLog));
FleckLog.LogAction = (level, message, ex) => {
switch(level) {
case LogLevel.Debug:
logger.Debug(message, ex);
break;
case LogLevel.Error:
logger.Error(message, ex);
break;
case LogLevel.Warn:
logger.Warn(message, ex);
break;
default:
logger.Info(message, ex);
break;
}
};
Set NoDelay
to true
on the WebSocketConnection.ListenerSocket
var server = new WebSocketServer("ws://0.0.0.0:8181");
server.ListenerSocket.NoDelay = true;
server.Start(socket =>
{
//Child connections will not use Nagle's Algorithm
});
Set RestartAfterListenError
to true
on the WebSocketConnection
var server = new WebSocketServer("ws://0.0.0.0:8181");
server.RestartAfterListenError = true;
server.Start(socket =>
{
//...use as normal
});
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。