gpt4 book ai didi

javascript - 带有 express 的 socket.io 更新 DOM 中的变量

转载 作者:行者123 更新时间:2023-11-30 05:46:06 26 4
gpt4 key购买 nike

这是测试用例...

应用程序.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

最佳答案

问题是端口

这是解决方案

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' });
});

关于javascript - 带有 express 的 socket.io 更新 DOM 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17893516/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com