gpt4 book ai didi

node.js - Faye 网络套接字

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

我是 faye web socket 的新手,我尝试设置服务器和客户端来交换信息。不幸的是,我遗漏了一些东西,我无法在客户端上获取从服务器发送的消息。这是我的 server.js:

     var faye = require('faye');

var Router = require('node-simple-router');
var router = new Router();
var server = require('http').createServer(router);
var port = 5000;

var bayeux = new faye.NodeAdapter({mount: '/faye/calls'});
bayeux.attach(server);

// Launch the server
server.listen(port, function() {
console.log('Listening on ' + port);
setInterval(function(){
bayeux.getClient().publish('/faye/calls', {text: 'Hello'});
},3000);

这是我的 client.js

         var faye = require('faye');

var client = new faye.Client('http://localhost:5000/faye', {
timeout: 600,
retry: 5
});

var sub = client.subscribe('/calls', function(message) {
console.log('message:' + message);
});

sub.then(function() {
console.log('overall Subscription is now active!');
});

有人可以告诉我我错过了什么吗?非常感谢

祝你有美好的一天

最佳答案

看起来你的路径是罪魁祸首:

var bayeux = new faye.NodeAdapter({mount: '/faye/calls'});

应该挂载到/faye:

var bayeux = new faye.NodeAdapter({mount: '/faye'});

bayeux.getClient().publish('/faye/calls', {text: 'Hello'});

实际上应该发布到/calls

bayeux.getClient().publish('/calls', {text: 'Hello'});

这似乎解决了我的问题:

overall Subscription is now active!
message:[object Object]
message:[object Object]
message:[object Object]

关于node.js - Faye 网络套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32190472/

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