【问题标题】:socket.io with express to update variables in the DOMsocket.io 使用 express 来更新 DOM 中的变量
【发布时间】:2013-07-27 01:51:59
【问题描述】:

这是测试用例...

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

【问题讨论】:

  • 为我工作.. 您在哪里看到这些错误?端口 26000 是什么?默认快递端口不是3000吗?
  • 我使用 26000,对我不起作用,我找不到问题,我在客户端看到这个错误,使用 chrome
  • 套接字正在尝试请求(未编码)/socket.io/1/websocket/<h2>file not found! error 404!</h2>。检查 npm ls 是否有任何缺失的依赖项。或者尝试删除并重新安装socket.io。注意:socket.io的依赖包括native addonws,需要安装node-gypits external dependencies
  • 26000 在 chrome 上也适用于我。我真的很想帮忙,但我无法传播错误。
  • 检查了所有依赖项,我发现了这个...当我将服务器上的 io 端口更改为 26001 和 io.connect('localhost:26001');在客户端它可以工作

标签: javascript node.js express socket.io


【解决方案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' });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-16
    • 2016-09-02
    • 2015-08-03
    • 2015-09-07
    • 2019-01-11
    • 2016-11-10
    • 1970-01-01
    • 2021-02-27
    相关资源
    最近更新 更多