我正在尝试使用socket.io设置服务器,结果不好.
我主要遵循本指南,尽管有些过时:
http://www.williammora.com/2013/03/nodejs-tutorial-building-chatroom-with.html
问题来自socket.io,我不知道它是客户端还是服务器端.似乎正在尝试不断地轮询服务器,但正在回到404.这听起来像socket.io没有运行,但是对我来说看起来都不错.它也可能与路径有关,并且有一个“公共”目录,但我不知道.
127.0.0.1 - - [Thu,17 Jul 2014 00:51:36 GMT] "GET /socket.io/?EIO=2&transport=polling&t=1405558296120-0 HTTP/1.1" 404 73 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.76.4 (KHTML,like Gecko) Version/7.0.4 Safari/537.76.4" 127.0.0.1 - - [Thu,17 Jul 2014 00:51:37 GMT] "GET /socket.io/?EIO=2&transport=polling&t=1405558297181-1 HTTP/1.1" 404 73 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.76.4 (KHTML,17 Jul 2014 00:51:39 GMT] "GET /socket.io/?EIO=2&transport=polling&t=1405558299207-2 HTTP/1.1" 404 73 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.76.4 (KHTML,like Gecko) Version/7.0.4 Safari/537.76.4"
服务器
var exec = require( "child_process" ).exec; var path = require( "path" ); var morgan = require( "morgan" ); var bodyParser = require( "body-parser" ); var _ = require( "underscore" ); var express = require( "express" ); var app = express(); var http = require( "http" ).createServer( app ); var io = require( "socket.io" )( http ); app.set( "ipaddr","127.0.0.1" ); app.set( "port",8080 ); //support JSON,urlencoded,and multipart requests app.use( bodyParser() ); //log the requests using morgan app.use( morgan() ); //specify the Jade views folder app.set( "views",__dirname + "/../views" ); //set the view engine to Jade app.set( "view engine","jade" ); //specify static content app.use( express[ "static" ]( "public",__dirname + "/../public" ) ); //using map-access of static so jslint won't bitch io.on( "connection",function( socket ) { console.log( "A user connected." ); });
客户
script( src="js/socket.io.js" ) var socket = io.connect( "http://localhost:8080" );
我从…获得客户端的js文件:
node_modules / socket.io / node_modules / socket.io客户端/ socket.io.js
该路径与我使用的教程所说的不同,或者socket.io使用的是什么,所以也许这是问题,但是它看起来可能是正确的文件.
有什么帮助吗
编辑
有关其他信息,以下是我的文件夹层次结构:
Webserver/ my_modules/ server.js node_modules/ body-parser/ express/ jade/ morgan/ socket.io/ underscore/ public/ css/ js/ server.js views/ index.jade index.js package.json
my_modules文件夹中的server.js是我在服务器端启动socket.io的地方. js文件夹中的server.js是从网站连接的客户端代码.