我有一個使用Node.js和WS包構建的非常簡單的WebSocket服務器。如何配置Ubuntu Server以使用Node.js進行WebSockets,並將Apache2用於其他所有內容?
var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({
port: 8080
});
wss.on('connection', function(client) {
client.on('message', function(message) {
console.log('received: %s', message);
});
client.send('something');
});
// Console will print the message
console.log('-=-=- Server running :8080 -=-=-');
當我嘗試使用簡單的客戶端連接時,連接超時。
<script>
var ws = new WebSocket('ws://192.168.1.75:8080');
ws.onconnect = function(){
console.log("Connected!");
};
</script>
服務器啓動/運行沒有錯誤,但客戶端總是超時連接。我是否需要配置iptables或apache以將端口8080上的流量發送到我寫的節點服務器?我想服務器會自動開始監聽端口8080.(如果這是一個成熟的問題,我會提前道歉)
您的服務器在8080上偵聽,您的客戶端連接到1947 ... –
這實際上是一個Typo。對於那個很抱歉。我嘗試更換端口,並忘記交換客戶端的問題。現在已經修復了。 –