gpt4 book ai didi

html - node.js socket.io 未向连接的客户端广播

转载 作者:太空宇宙 更新时间:2023-11-04 00:18:40 24 4
gpt4 key购买 nike

我有一个从程序接收消息的应用程序。该应用程序接收消息,然后将消息广播到连接的客户端。现在我在控制台和控制台上显示消息,该接收器应用程序正在完美接收。但是在客户端(html 页面)上,它没有被广播。当我打开 localhost/result 时,没有显示任何内容。我做错了什么?

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var EventHubClient = require('azure-event-hubs').Client;
var connectionString = 'connection string';

const bodyParser = require('body-parser')

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

var printError = function (err) {
console.log(err.message);
};

var result;

var printMessage = function (message) {
console.log('Message received: ');
result = JSON.stringify(message.body);
obj = JSON.parse(result);
console.log('message:' + result)

console.log('');
};

count =0;

app.get('/result', function(req, res){

res.sendFile(__dirname + '/result.html');
});


io.on('connection', function(socket){
console.log('user connected');

socket.on('chat message', function(msg){

io.emit('chat message', result);
console.log("This is id in the sock' section" + check_id);
});
socket.on('disconnect', function(){
console.log('user disconnected');
socket.removeAllListeners('disconnect');
io.removeAllListeners('connection');
});
});

var client = EventHubClient.fromConnectionString(connectionString);
client.open()
.then(client.getPartitionIds.bind(client))
.then(function (partitionIds) {
return partitionIds.map(function (partitionId) {
return client.createReceiver('$Default', partitionId, { 'startAfterTime' : Date.now()}).then(function(receiver) {
console.log('Created partition receiver: ' + partitionId)
receiver.on('errorReceived', printError);
receiver.on('message', printMessage);
});
});
})
.catch(printError);


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

结果.html

<html>
<head><title>Hello world</title></head>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
// socket.emit('chat message')


socket.on('chat message',function(msg){

socket.emit('chat message',msg);
document.write('hello world');
document.write(msg);
});
</script>
</html>

最佳答案

我会直接从 printMessage 函数发送它:

function printMessage(message) {
result = JSON.stringify(message.body);
console.log('[message]',result);
io.sockets.emit('chat message',result);
}

并记住修改您的客户端 (result.html),以免造成无限循环:

<script>
var socket=io();
socket.on('chat message',function(msg){
console.log(msg); //alert(msg);
document.write(msg);
});
</script>
<小时/>

编辑:

如何包含 socket.io.js 脚本?

<script src="/socket.io/socket.io.js"></script>

尝试指定服务器地址socket.connect('localhost:3000');

I made this working example for you

<小时/>

重新编辑: I remade the working example for you, this time I added a client-server emission that bounces back to all connected clients .

关于html - node.js socket.io 未向连接的客户端广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45453888/

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