最近项目中使用WebSocket建立前后端通讯,是通过SuperSocket.WebSocket实现的,在前端向后端发送请求或者数据的时候,后端报了一个异常:
Max request length: 1024, current processed length: 1790
.
经过摸索,发现是SuperSocket对请求信息的最大长度有所限制,默认请求最大长度为1024. 该配置可以通过一个ServerConfig的对象进行设置,ServerConfig对象中默认配置如下:
//
// Summary:
// Default ReceiveBufferSize
public const int DefaultReceiveBufferSize = 4096;
//
// Summary:
// The default listen backlog
public const int DefaultListenBacklog = 100;
//
// Summary:
// The default keep alive time
public const int DefaultKeepAliveTime = 600;
//
// Summary:
// The default session snapshot interval
public const int DefaultSessionSnapshotInterval = 5;
//
// Summary:
// The default send buffer size
public const int DefaultSendBufferSize = 2048;
//
// Summary:
// Default idle session timeout
public const int DefaultIdleSessionTimeOut = 300;
//
// Summary:
// The default keep alive interval
public const int DefaultKeepAliveInterval = 60;
//
// Summary:
// Default send timeout value, in milliseconds
public const int DefaultSendTimeout = 5000;
//
// Summary:
// Default MaxRequestLength
public const int DefaultMaxRequestLength = 1024;
//
// Summary:
// Default sending queue size
public const int DefaultSendingQueueSize = 5;
//
// Summary:
// Default MaxConnectionNumber
public const int DefaultMaxConnectionNumber = 100;
//
// Summary:
// Default clear idle session interval
public const int DefaultClearIdleSessionInterval = 120;
...
修改这个最大值限制也比较容易,即在websocketServer 创建连接的时候,new一个ServerConfig对象,在serverConfig对象中修改MaxRequestLength即可,实现的代码如下:
_webSocketServer = new WebSocketServer();
var config = new ServerConfig() {
MaxRequestLength = 1024 * 1024 , // 限制请求的数据长度最大为1M
Ip = WebSocketIp,
Port = WebSocketPort
};
_webSocketServer.Setup(config, logFactory: new WebSocketLogFactory());