Socket.io 未由 Node.js 服务器提供服务 [英] Socket.io not being served by Node.js server
本文介绍了Socket.io 未由 Node.js 服务器提供服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
据我所知,来自 http://socket.io/#how-to-use,节点.js 自动在服务器上提供 socket.io 文件.
As I understood it, from http://socket.io/#how-to-use, node.js automatically serves the socket.io file on the server.
我已经使用 npm install socket.io
安装了 socket.io,我可以看到它位于服务器根目录之上一层的 node_modules
中.
I have installed socket.io with npm install socket.io
and I can see that it resides in node_modules
one level above the server root.
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:
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 时,我收到以下错误:
Starting the server works fine, but when opening index.html, I receive the following error:
GET http://localhost:8253/socket.io/socket.io.js 404 (Not Found)
Uncaught ReferenceError: io is not defined :8253/:25
想法?
推荐答案
用socket.io绑定服务器后尝试监听
Try listening on the server after you bind it with socket.io
放置这个
httpServer.listen(8253);
之后
var webSocket = socketIO.listen(httpServer);
这篇关于Socket.io 未由 Node.js 服务器提供服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文