2017-02-18 116 views
-1

我在使用以下代碼連接到套接字io和express服務器時遇到了很多問題。 我可以連接到瀏覽器的網頁/ api,但只要我嘗試從index.html連接套接字連接,我得到的錯誤如下嘗試連接到套接字io/express服務器時出現404錯誤

這是我的'server.js'

var WebSocketServer = require('uws').Server, 
    express   = require('express'), 
    path   = require('path'), 
    app    = express(), 
    server   = require('http').createServer(), 
    createEngine = require('node-twig').createEngine; 

app.engine('.twig', createEngine({ 
    root: __dirname + '/public', 
})); 

app.set('views', './public'); 
app.set('view engine', 'twig'); 

var cors = require('cors'); 
app.use(cors()); 

app.use(require('./routes')); 
app.use(require('./api')); 

app.use(express.static(path.join(__dirname, '/public'))); 

app.use("/admin", express.static(__dirname + '/public')); 
app.use("/admin/scss", express.static(__dirname + '/scss')); 
app.use("/admin/vendors", express.static(__dirname + '/vendors')); 
app.use("/admin/js", express.static(__dirname + '/js')); 
app.use("/admin/build", express.static(__dirname + '/build')); 

app.use("/scss", express.static(__dirname + '/scss')); 
app.use("/vendors", express.static(__dirname + '/vendors')); 
app.use("/js", express.static(__dirname + '/js')); 
app.use("/build", express.static(__dirname + '/build')); 

var wss = new WebSocketServer({server: server}); 

wss.on('connection', function (ws) { 

    ws.on('join', function() { 
     console.log('SOMEONE JUST JOINED'); 
    }); 

    ws.on('close', function() { 
     console.log('stopping client interval'); 
     clearInterval(id); 
    }); 
}); 

server.on('request', app); 

server.listen(8080, function() { 
    console.log('Listening on http://localhost:8080'); 
}); 

,這是我的index.html文件:

<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> 

<script type="text/javascript"> 

var so = io.connect('http://192.168.***.***:8080/'); 

so.on('connect',() => { 
    so.emit('join'); 
}); 

</scrip> 

這是我不斷收到錯誤:

XMLHttpRequest cannot load http://localhost:8080/socket.io/?EIO=3&transport=polling&t=LfHXP1O. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://192.168.4.149:8080' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute. 

,有時我得到這個錯誤:

Failed to load resource: the server responded with a status of 404 (Not Found) 

解決方案:

<script> 
     var host = window.document.location.host.replace(/:.*/, ''); 
     var server = new WebSocket('ws://' + host + ':8080'); 
     server.onmessage = function (event) { 
     updateStats(JSON.parse(event.data)); 
     }; 

     server.onopen = function (event) {}; 


    </script> 

回答

0

您正在嘗試使用socket.io在客戶端連接到後端的一個普通的WebSocket服務器。你不能那樣做。 socket.io是一個位於webSocket之上的圖層,因此您需要使用普通的webSocket連接到客戶端中的普通webSocket服務器或socket.io,以連接到後端的socket.io服務器。

我建議你改變你的服務器來使用socket.io作爲socket.io服務器而不是普通的webSocket服務器。

您得到的錯誤是因爲socket.io客戶端期望能夠發出socket.io服務器將處理的某個http請求,但由於您沒有socket.io服務器,因此URL請求不在服務器上處理。

+0

你好。感謝您的回覆。 1.我通過var wss = new WebSocketServer({server:server});是後端socket.io的一個實現,但只是進行了優化。 2.說我想保持服務器的狀態,那我究竟能夠從客戶端連接到它? 對於所有的移植和websockets等我都很新 – TheMan68

相關問題