簡體   English   中英

Node.js + Express + socket.io-socket.io無法正常運行

[英]Node.js + Express + socket.io - socket.io not serving properly

我知道這是一個非常討論的話題,但我似乎仍然無法使它探索Internet上的每個解決方案。 我的app.js代碼如下所示:

var express = require('express');
var io = require('socket.io');

var app = express();
app.use(express.static(__dirname + '/public'));

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


/***************************************/
/*********** START SERVER **************/
/***************************************/
var server = app.listen(3000, '0.0.0.0', function() {
  console.log('Listening on port %d', server.address().port);
});
io = io.listen(server);

/***************************************/
/*********** COMMUNICATIONS ************/
/***************************************/
io.sockets.on('connection', function(socket){..... //continues

index.jade文件的相關部分:

script(type='text/javascript' src='/socket.io/socket.io.js')
script(type='text/javascript' src='http://code.jquery.com/jquery-1.10.2.min.js')
script(type='text/javascript' src='/javascripts/index.js')

我的index.js看起來像這樣:

//init the connection with the server
var socket = io.connect('/');

socket.on('message', function(message){
    //parse message
    message = JSON.parse(message);
    alert(message);
});

$(function (){
    var data = {message: 'test test test'};
    socket.send(JSON.stringify(data));
});

在chrome開發人員控制台中查看時,出現以下錯誤:

socket.io.js:2935 GET http://arielschon12.koding.io/socket.io/?EIO=3&transport=polling&t=1423347369182-0 
arielschon12.koding.io/:1 XMLHttpRequest cannot load http://arielschon12.koding.io/socket.io/?EIO=3&transport=polling&t=1423347369182-0. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://arielschon12.koding.io:3000' is therefore not allowed access. The response had HTTP status code 404.
socket.io.js:2935 GET http://arielschon12.koding.io/socket.io/?EIO=3&transport=polling&t=1423347370898-1 
arielschon12.koding.io/:1 XMLHttpRequest cannot load http://arielschon12.koding.io/socket.io/?EIO=3&transport=polling&t=1423347370898-1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://arielschon12.koding.io:3000' is therefore not allowed access. The response had HTTP status code 404.
socket.io.js:2935 GET http://arielschon12.koding.io/socket.io/?EIO=3&transport=polling&t=1423347372842-2 
arielschon12.koding.io/:1 XMLHttpRequest cannot load http://arielschon12.koding.io/socket.io/?EIO=3&transport=polling&t=1423347372842-2. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://arielschon12.koding.io:3000' is therefore not allowed access. The response had HTTP status code 404.

每隔幾秒鍾就會彈出更多相同類型的錯誤。.我不知道該怎么辦。 任何幫助表示贊賞!

在客戶端,您需要:

var socket =io();
socket.on('connect',function(){});

和服務器端:

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

server.listen(80);

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM