2015-06-27 63 views
0

我想連接本地主機上的一個wamp服務器,這是一個html頁面。socket.io http在本地主機上監聽

到目前爲止,我有這樣的:

//SOCKET.IO Setup 
var app = require('express')(); 
var http = require('http').Server(app); 
var io = require('socket.io')(http); //initialise after http server 

io.on('connection', function(socket){ 
    console.log('a user connected'); 
    socket.on('disconnect', function(){ 
    console.log('user disconnected'); 
    }); 
}); 
http.listen('/wheel', function(){ 
    console.log('listening on */wheel'); 
}); 

app.get('/', function(req, res){ 
    res.sendfile('wheel.html'); 
}); 

我的HTML頁面WWW /車輪內,HTML頁面被稱爲wheel.html

這是我的錯誤:

[17:08:51] error - Error: listen EACCES 
    at exports._errnoException (util.js:746:11) 
    at Server._listen2 (net.js:1139:19) 
    at listen (net.js:1182:10) 
    at Server.listen (net.js:1261:5) 
    at Object.<anonymous> (C:\Family\wheel.js:14:6) 
    at Module._compile (module.js:460:26) 
    at Object.Module._extensions..js (module.js:478:10) 
    at Module.load (module.js:355:32) 
    at Function.Module._load (module.js:310:12) 
    at Function.Module.runMain (module.js:501:10) 

如何使用socket.io正確連接到本地主機中的Web目錄?它的工作原理,當我使用套接字和端口:

http.listen(1000, function(){ 
    console.log('listening on *:1000'); 
}); 

app.get('/', function(req, res){ 
    res.sendfile('wheel.html'); 
}); 

回答

1

的問題是,你正在努力使HTTP服務器偵聽一個名爲/wheelUNIX socket,而你沒有正確的權限創建文件。

如果你希望能夠服務於HTML文件時,客戶端服務器(其中,例如,在3000端口監聽)上呼籲/

http.listen(3000); 
... 
app.get('/', function(req, res){ 
    res.sendfile('./www/wheel/wheel.html'); 
}); 
+0

嘿!這更有意義,謝謝。我現在主要關心的是我將nodule_modules移動到/ www /目錄,因爲html文件需要'socket.io/socket.io.js'。但是當我查看/node_modules/socket.io時,沒有socket.io.js文件。但它是在C:\ wamp \ www \ node_modules \ socket.io \ node_modules \ socket.io.js裏面 –

+0

基本上,我不能將路徑指向wamp目錄www? –

+0

@BradlySpicer socket.io會自動將請求「捕獲」到客戶端.js文件併爲您處理,您不必爲此做任何事情。對於通用的靜態文件處理,您可能需要查看Express'['static'](http://expressjs.com/starter/static-files.html)中間件。 – robertklep

相關問題