我想通過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