我无法设置socket.io

时间:2016-09-07 14:29:28

标签: javascript node.js sockets socket.io

所以我决定使用socket.io,将数据从服务器端发送到客户端,但它给了我这个错误:

VM134:3511 GET http://[url]/socket.io/?EIO=3&transport=polling&t=LS52PFM 404 (Not Found)

和:

Failed to load resource: the server responded with a status of 404 (Not Found)

这是我的app.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var login = require('./routes/login');
var facebook = require('./routes/facebook');
var dev = require('./routes/dev');
var users = require('./routes/users');
var ux = require('./routes/ux');
var session = require('express-session');
//Maziar added
var addPlaces = require('./routes/addPlaces');


// socket.io stuff : 
var app = express()
  , http = require('http')
  , server = http.createServer(app)
  , io = require('socket.io').listen(server);

index.js:

// io socket{
var app = require('express')();
var http = require('http').Server(app);



//io socket end. }
function emit() { io.sockets.emit('reload'); };

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

我的客户端代码:

var socket = io();
socket.on("reload", function(r) { alert('love you!'); });

在我添加的html部分中:

<script src="scripts/socket.io.js"></script>
<script src="https://cdn.socket.io/socket.io-1.0.0.js"></script>

(我还在本地下载了socket.io.js文件,以确保它可以访问它!)

Package.json:

  "dependencies": {
    "body-parser": "~1.13.2",
    "chance": "^1.0.3",
    "connect-ensure-login": "^0.1.1",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "express": "~4.13.1",
    "express-session": "^1.13.0",
    "hjs": "~0.0.6",
    "mongodb": "^2.1.19",
    "morgan": "~1.6.1",
    "passport": "^0.3.2",
    "passport-local": "^1.0.0",
    "request": "^2.72.0",
    "serve-favicon": "~2.3.0",
    "util": "^0.10.3",
    "socket.io": "1.2.0"
  }

临时搭建:

 /tmp/build_39cf5617a994685d99e4b2a55679a42b
remote:        ├── chance@1.0.4
remote:        ├── connect-ensure-login@0.1.1
remote:        ├─┬ express-session@1.14.1
remote:        │ ├── cookie@0.3.1
remote:        │ ├── cookie-signature@1.0.6
remote:        │ ├── crc@3.4.0
remote:        │ ├── depd@1.1.0
remote:        │ ├── on-headers@1.0.1
remote:        │ ├── parseurl@1.3.1
remote:        │ ├─┬ uid-safe@2.1.2
remote:        │ │ ├── base64-url@1.3.2
remote:        │ │ └── random-bytes@1.0.0
remote:        │ └── utils-merge@1.0.0
remote:        ├─┬ mongodb@2.2.9
remote:        │ ├── es6-promise@3.2.1
remote:        │ ├─┬ mongodb-core@2.0.11
remote:        │ │ ├── bson@0.5.4
remote:        │ │ └─┬ require_optional@1.0.0
remote:        │ │   ├── resolve-from@2.0.0
remote:        │ │   └── semver@5.3.0
remote:        │ └─┬ readable-stream@2.1.5
remote:        │   ├── buffer-shims@1.0.0
remote:        │   ├── core-util-is@1.0.2
remote:        │   ├── inherits@2.0.3
remote:        │   ├── isarray@1.0.0
remote:        │   ├── process-nextick-args@1.0.7
remote:        │   ├── string_decoder@0.10.31
remote:        │   └── util-deprecate@1.0.2
remote:        ├─┬ passport@0.3.2
remote:        │ ├── passport-strategy@1.0.0
remote:        │ └── pause@0.0.1
remote:        ├── passport-local@1.0.0
remote:        └─┬ socket.io@1.4.8
remote:        ├─┬ engine.io@1.6.11
remote:        │ ├─┬ accepts@1.1.4
remote:        │ │ ├─┬ mime-types@2.0.14
remote:        │ │ │ └── mime-db@1.12.0
remote:        │ │ └── negotiator@0.4.9
remote:        │ ├── base64id@0.1.0
remote:        │ ├─┬ engine.io-parser@1.2.4
remote:        │ │ ├── after@0.8.1
remote:        │ │ ├── arraybuffer.slice@0.0.6
remote:        │ │ ├── base64-arraybuffer@0.1.2
remote:        │ │ ├── blob@0.0.4
remote:        │ │ ├─┬ has-binary@0.1.6
remote:        │ │ │ └── isarray@0.0.1
remote:        │ │ └── utf8@2.1.0
remote:        │ └─┬ ws@1.1.0
remote:        │   ├── options@0.0.6
remote:        │   └── ultron@1.0.2
remote:        ├─┬ has-binary@0.1.7
remote:        │ └── isarray@0.0.1
remote:        ├─┬ socket.io-adapter@0.4.0
remote:        │ └─┬ socket.io-parser@2.2.2
remote:        │   ├── debug@0.7.4
remote:        │   ├── isarray@0.0.1
remote:        │   └── json3@3.2.6
remote:        ├─┬ socket.io-client@1.4.8
remote:        │ ├── backo2@1.0.2
remote:        │ ├── component-bind@1.0.0
remote:        │ ├── component-emitter@1.2.0
remote:        │ ├─┬ engine.io-client@1.6.11
remote:        │ │ ├── component-inherit@0.0.3
remote:        │ │ ├── has-cors@1.1.0
remote:        │ │ ├── parsejson@0.0.1
remote:        │ │ ├── parseqs@0.0.2
remote:        │ │ ├── ws@1.0.1
remote:        │ │ ├── xmlhttprequest-ssl@1.5.1
remote:        │ │ └── yeast@0.1.2
remote:        │ ├── indexof@0.0.1
remote:        │ ├── object-component@0.0.3
remote:        │ ├─┬ parseuri@0.0.4
remote:        │ │ └─┬ better-assert@1.0.2
remote:        │ │   └── callsite@1.0.0
remote:        │ └── to-array@0.1.4
remote:        └─┬ socket.io-parser@2.2.6
remote:        ├── benchmark@1.0.0
remote:        ├── component-emitter@1.1.2
remote:        ├── isarray@0.0.1
remote:        └── json3@3.3.2

错误:在控制台中:

at=info method=GET path="/socket.io/socket.io.js" host=HOST request_id=71077d84-55ec-4f02-b2bf-8dd79c29ac89 fwd="84.208.103.77" dyno=web.1 connect=1ms service=6ms status=404 bytes=243

0 个答案:

没有答案