我想通过nginx与ssl设置socketio。问题是我可以让客户端连接,但是我没有看到我期望通过套接字发送的其他事件。 (注意:这不会工作在当地只是不是我的生产服务器上)通过Nginx与SSL的SocketIO
客户端代码是在这里:
import openSocket from "socket.io-client";
const socket = openSocket(`${SOCKET}`);
function subscribeToTimer(callBack) {
socket.on("timer", timestamp => callBack(null, timestamp));
socket.emit("subscribeToTimer", 1000);
}
export class App extends Component {
constructor(props) {
super(props);
this.store = this.configureStore();
subscribeToTimer((err, action) => this.store.dispatch(action));
}
和服务器:
const port = 8000
const io = require('socket.io')()
io.on("connection", (client) => {
console.log("a user connected")
client.on("subscribeToTimer", (interval) => {
console.log("a user is subscribing to timer with interval: ", interval)
setInterval(() => {
timestamp = new Date()
client.emit('timer', { type: 'SET_TIME', payload: timestamp });
}, interval);
});
})
io.listen(port)
console.log('listening on port ', port)
这是由nginx的/etc/nginx/sites-enabled/default
管理:
server {
<snip>
location /socket.io {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
当我启动服务器时,我得到:
listening on port 8000
a user connected
所以,客户端连接到服务器,但我没有看到subscribeToTImer
事件。
这里的任何见解?
开发者控制台中的任何内容? –
@TarunLalwani客户端控制台中没有任何东西。我其实已经想通了,所以我会更新一个答案:) – user341493