socket.io和网站使用同一个端口会报错400,有什么办法可以使用同一端口吗?
java代码
public SocketIOServer socketIOServer() throws Exception {
SocketConfig socketConfig = new SocketConfig();
socketConfig.setTcpNoDelay(true);
socketConfig.setSoLinger(0);
com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
config.setSocketConfig(socketConfig);
config.setHostname(localhost);
config.setPort(8080);
config.setBossThreads(1);
config.setWorkerThreads(2000);
config.setAllowCustomRequests(true);
config.setUpgradeTimeout(1000000);
config.setPingTimeout(6000000);
config.setPingInterval(25000);
return new SocketIOServer(config);
}
前端代码
var serverUri ="http://localhost:8080";
var sendEvent = "ServerReceive";
var receiveEvent = "ClientReceive";
var socket;
connect(1001);
function connect(uid) {
socket = io.connect(serverUri, {
'force new connection': true,
'query': 'UID=' + uid
});
socket.on('connect', function () {
console.log("连接成功");
socket.on(receiveEvent, function (data) {
console.log("收到消息");
});
socket.on('disconnect', function () {
console.log("连接断开");
});
}
function send(data) {
socket.emit(sendEvent, data);
}