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