【发布时间】:2017-09-04 19:59:52
【问题描述】:
我正在尝试通过 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 客户端控制台中没有任何内容。我实际上想通了,所以我会更新答案:)