Express 服务器被套接字阻塞
Express server blocked with Sockets
我有一个问题,我正在使用 Sockets(模块 socketcluster-server)安装一个 express 服务器,但是当我发送 http 请求时,express 在大约 20 个请求后被阻止,导致 Sockets(客户端)通知他们有 运行 连接。
你遇到过什么人吗?有什么想法可以帮助我解决这个问题吗?
谢谢
express.js
const http = require('http'),
express = require('express'),
socket = require('./socket'),
HOST = 'localhost',
PORT = 8000,
bodyParser = require('body-parser');
const app = express()
const httpServer = http.createServer(app);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/',(req,res)=>res.send({status:"ok"}))
socket.init(httpServer,HOST,PORT)
httpServer.listen(PORT,HOST);
httpServer.on('listening', () =>{
console.log('Express server started on port %s at %s', httpServer.address().port, httpServer.address().address);
});
socket.js
const socketClusterServer = require('socketcluster-server')
module.exports = (() => {
let scServer
const createInstance = (httpServer, host, port) => {
if (!scServer)
scServer = socketClusterServer.attach(httpServer, {host, port});
scServer.on('connection', function (socket) {
console.log('socker: ', scServer.clientsCount)
socket.on('client',getMessageByClient)
})
}
const getMessageByClient = (data) => {
if (data.channel)
sendMessage(data.channel, data)
scServer.removeListener('client', getMessageByClient)
}
const sendMessage = (channel, message) => scServer.exchange.publish(channel, JSON.stringify(message))
const close = () => scServer.close()
return {
init: (httpServer, host, port) => createInstance(httpServer, host, port),
sendMessage: (channel, message) => sendMessage(channel, message),
close: () => close()
}
})()
最终在尝试了很多方法后,你会尝试如下解决这个问题:
// Environment variables
const { env: { HOST, PORT }} = process
//Create http server
const httpServer = require('http')
//Init express
const express = require('express')
const app = express()
// Here would come express configurations and routes
// Attach express to our httpServer
httpServer.on('request', app)
// Init socket with http server
const socket = require('./socket')
socket.init(httpServer, HOST)
// Init http server
httpServer.listen(PORT, HOST)
httpServer.on('listening', async () => {
console.info(`Server started on port ${httpServer.address().port} at ${httpServer.address().address}`)
})
目前它工作正常,它接收到明确的请求和套接字,没有任何阻塞。
希望对大家有所帮助。
我有一个问题,我正在使用 Sockets(模块 socketcluster-server)安装一个 express 服务器,但是当我发送 http 请求时,express 在大约 20 个请求后被阻止,导致 Sockets(客户端)通知他们有 运行 连接。
你遇到过什么人吗?有什么想法可以帮助我解决这个问题吗?
谢谢
express.js
const http = require('http'),
express = require('express'),
socket = require('./socket'),
HOST = 'localhost',
PORT = 8000,
bodyParser = require('body-parser');
const app = express()
const httpServer = http.createServer(app);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/',(req,res)=>res.send({status:"ok"}))
socket.init(httpServer,HOST,PORT)
httpServer.listen(PORT,HOST);
httpServer.on('listening', () =>{
console.log('Express server started on port %s at %s', httpServer.address().port, httpServer.address().address);
});
socket.js
const socketClusterServer = require('socketcluster-server')
module.exports = (() => {
let scServer
const createInstance = (httpServer, host, port) => {
if (!scServer)
scServer = socketClusterServer.attach(httpServer, {host, port});
scServer.on('connection', function (socket) {
console.log('socker: ', scServer.clientsCount)
socket.on('client',getMessageByClient)
})
}
const getMessageByClient = (data) => {
if (data.channel)
sendMessage(data.channel, data)
scServer.removeListener('client', getMessageByClient)
}
const sendMessage = (channel, message) => scServer.exchange.publish(channel, JSON.stringify(message))
const close = () => scServer.close()
return {
init: (httpServer, host, port) => createInstance(httpServer, host, port),
sendMessage: (channel, message) => sendMessage(channel, message),
close: () => close()
}
})()
最终在尝试了很多方法后,你会尝试如下解决这个问题:
// Environment variables
const { env: { HOST, PORT }} = process
//Create http server
const httpServer = require('http')
//Init express
const express = require('express')
const app = express()
// Here would come express configurations and routes
// Attach express to our httpServer
httpServer.on('request', app)
// Init socket with http server
const socket = require('./socket')
socket.init(httpServer, HOST)
// Init http server
httpServer.listen(PORT, HOST)
httpServer.on('listening', async () => {
console.info(`Server started on port ${httpServer.address().port} at ${httpServer.address().address}`)
})
目前它工作正常,它接收到明确的请求和套接字,没有任何阻塞。
希望对大家有所帮助。