2011-12-24 135 views
6

據我所知,從http://socket.io/#how-to-use,node.js自動服務於服務器上的socket.io文件。Socket.io沒有被Node.js服務器服務

我已經安裝了socket.io與npm install socket.io,我可以看到它駐留在服務器根目錄上一級的node_modules

server.js:

var static = require('./plugins/node-static'); 
var socketIO = require('socket.io'); 
var clientFiles = new static.Server('./client'); 

var http = require('http'); 
httpServer = http.createServer(function (request, response) { 
    request.addListener('end', function() { 
      clientFiles.serve(request, response); 
     }); 
}).listen(8253); 

var webSocket = socketIO.listen(httpServer); 
webSocket.on('connection', function(client) { ..... 

的index.html:

<html> 
<head> 
    <title>Chat</title> 
</head> 
<body> 
    <script src="/socket.io/socket.io.js"></script> 
    <script type="text/javascript" 
      src="http://code.jquery.com/jquery-1.5.2.js"></script> 

    <script type="text/javascript"> 
     $(document).ready(function() { 
      var webSocket = new io.Socket('localhost', { port: 8253 }); 
      webSocket.connect(); ....... 

啓動服務器工作正常,但打開index.html的時候,我收到以下錯誤:

GET http://localhost:8253/socket.io/socket.io.js 404 (Not Found) 
Uncaught ReferenceError: io is not defined     :8253/:25 

想法?

+0

「我可以看到它駐留在服務器根目錄上一級的node_modules中。」 「服務器根」是什麼意思?也許不會與apache的任何痕跡混淆? NPM安裝的模塊駐留在NPM倉庫中,它們不必位於與項目的主節點腳本相關的任何地方。成功的'require('socket.io')'表示安裝正常,問題出在代碼中。 – Kos 2011-12-24 23:09:31

回答

6

試着聽聽在服務器上後,您將其綁定與socket.io

httpServer.listen(8253); 

將這個

var webSocket = socketIO.listen(httpServer); 
3

編輯:道歉,我寫了一些沒有回答你的問題。

在您需要以下客戶端:

var socket = io.connect(); //Hostname and port not required - Autodetected 
socket.on('connect', function(){ 
    $('#status').text('Connected'); 
}); 
socket.on('message', function(m){ 
$('#message').text(m); 
}); 
socket.on('disconnect', function(){ 
$('#status').text('Disconnected'); 
}); 

工作實例=>https://github.com/parj/node-websocket-demo/blob/master/public/main.js

NPM信息(如果需要):如果你在Linux下

cd <location of your server.js> 
npm install -g socket.ion #install globally 
npm link socket.io. #Create a symbolic link 

如果您在Windows上你不能做npm鏈接

cd <location of your server.js> 
npm install socket.io 

你的目錄結構應該像

server.js 
node_modules/ #Directory - same level as server.js 
    socket.io #socket.io underneath that 

node_modules應該在同一目錄server.js,不高於服務器的根

+0

我不記得自己需要執行這樣的魔法,介紹教程也沒有提示。 'npm install socket.io'應該訣竅 – Kos 2011-12-24 23:04:36

+0

順便說一句,「如果你在Windows上」,現在npm支持windows嗎? – Kos 2011-12-24 23:04:47

+0

1/npm在Windows上=> cf stackoverflow文章 - http://stackoverflow.com/questions/7300132/how-to-use-npm-with-node-exe 2/Magic? :)教程討論npm安裝。他們錯過了什麼,如果你有幾個項目有同一個庫。那麼對於每個項目你去運行npm install?更好的解決方案(imo)是在全局安裝'npm install -g',然後創建一個鏈接到'npm link'。這樣你只需要在一個地方安裝並升級一個地方。 – 2011-12-25 06:46:23

-1

對於在Azure上部署的用戶(我無法爲任何其他平臺擔保),請確保您的package.json文件包含啓動腳本。

例子:

"scripts" : { "start": "node index.js" } 
+0

這個提示是有效的,但不是真的這個問題。 Azure對Node應用程序有特定的要求,但這幾乎與socket.io無關。 – kubal5003 2017-09-23 20:13:15

1

當你從一個普通快遞的應用轉換:

一(一個我相信每個人都有:

const express = require('express') 
const app = express() 
app.listen(3000, function() { 
    console.log('Example app listening on port 3000!') 
}) 

它做兩件事情是非常重要的右):

var server = require('http').Server(app); 
var io = require('socket.io')(server); 

二(這個人是很容易錯過): 呼叫的server.listen代替app.listen

我花了近兩個小時的調試這一點,這就是爲什麼我記錄。