2

これはテストケースです...

app.js

var express = require('express'),
    http = require('http'),
    //other variables

var app = express();
var server = http.createServer(app);


var io = require("socket.io").listen(server)
io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
});

html_index.js

$(document).ready(function()
{
    var socket = io.connect();
        socket.on('news', function (data) {
          console.log(data);
        });
});

このエラーが発生します

WebSocket connection to 'ws://localhost:26000/socket.io/1/websocket/%3Ch2%3Efile%20not%20found!%20error%20404!%3C/h2%3E' failed: WebSocket is closed before the connection is established. socket.io.js:2438
WS.close socket.io.js:2438
Socket.onDisconnect socket.io.js:1967
Transport.onDisconnect socket.io.js:1362
Transport.onClose socket.io.js:1456
(anonymous function)

使っています

express 3
socket.io 0.9
nodejs 0.10
4

1 に答える 1

1

問題はポートでした

これが解決策です

var server = app.listen(26000, function(){ //instead of var server = http.createServer(app);
    console.log("Express server listening on port %d in %s mode", app.get('port'),
    app.settings.env);
});
//SOCKET.IO
var io = require("socket.io").listen(server)
io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
});
于 2013-07-27T03:30:44.123 に答える