- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我们有一个用 HapiJS 实现的 REST 服务器和一个用 Socket.IO 实现的 Websockets 服务器(它们都在一个服务器上运行,但在不同的端口上)。我想从 HapiJS 服务器通知 Websockets 服务器将带有一些数据的事件发送到特定客户端。
套接字服务器在端口 8081 上运行,REST 在 8080 上。
这个想法是客户端执行一个操作(POST 请求),该操作记录在“操作历史记录”表中。该操作涉及其他用户,因此当发生这种情况时应实时通知他们。这就是其他用户监听 websocket 连接的原因。
我如何告诉套接字服务器向特定客户端发出事件并且应该从 REST 服务器完成?
我当时想了3种方法:
我尝试执行 Socket.IO-Emitter但它需要 Redis 数据库(我仍然不知道为什么)。当我尝试使用 HapiJS 路由处理程序中的发射器连接到套接字时,我得到:
export function* postRefreshEvent(userId) {
var connection = require('socket.io-emitter')({ host: '127.0.0.1', port: 8081 });
connection.in('UserHistory').emit('refresh', userId);
return {statusCode: OK}
}
Error: Ready check failed: Redis connection gone from end event.
在 RedisClient.on_info_cmd
Socket 服务器不执行刷新。我只是没有看到显示的日志。
样本 GIST .
你想过这样的事情吗?感谢您的帮助!
最佳答案
您可以只使用普通的旧 EventEmitter 在代码库的 socket.io 和 hapi 部分之间进行通信。这是一个有效的示例,并说明了如何执行此操作:
var Hapi = require('hapi');
// Make an event emitter for managing communication
// between hapi and socket.io code
var EventEmitter = require('events');
var notifier = new EventEmitter();
// Setup API + WS server with hapi
var server = new Hapi.Server();
server.register(require('inert'), function () {});
server.connection({ port: 4000, labels: ['api'] });
server.connection({ port: 4001, labels: ['ws'] });
var apiServer = server.select('api');
var wsServer = server.select('ws');
apiServer.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply.file('index.html');
}
});
apiServer.route({
method: 'GET',
path: '/action',
handler: function (request, reply) {
notifier.emit('action', { time: Date.now() });
reply('ok');
}
});
// Setup websocket stuff
var io = require('socket.io')(wsServer.listener);
io.on('connection', function (socket) {
// Subscribe this socket to `action` events
notifier.on('action', function (action) {
socket.emit('action', action);
});
});
server.start(function () {
console.log('Server started');
});
这是客户端的基本 index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://localhost:4001/socket.io/socket.io.js"></script>
</head>
<body>
<script>
var socket = io('http://localhost:4001');
socket.on('action', function (action) {
console.log(action);
});
</script>
</body>
</html>
如果你运行它并浏览到 http://localhost:4000
并打开你的控制台,你就可以向 http://localhost:4000/action
发出请求> 使用浏览器或使用 cURL (curl http://localhost:4000/action),您将看到事件出现在网络控制台中:
关于node.js - Socket.IO 与 HapiJS 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32760678/
基于 socket.io 的官方网站 http://socket.io/#how-to-use , 我找不到任何术语。socket.emit 、 socket.on 和 socket.send 之间有
我正在使用 lua-socket 3.0rc1.3(Ubuntu Trusty 附带)和 lua 5.1。我正在尝试监听 unix 域套接字,我能找到的唯一示例代码是 this -- send std
这两者有什么区别? 我注意到如果我在一个工作程序中从 socket.emit 更改为 socket.send ,服务器无法接收到消息,虽然我不明白为什么。 我还注意到,在我的程序中,如果我从 sock
使用套接字在两台服务器之间发送数据是个好主意,还是应该使用 MQ 之类的东西来移动数据。 我的问题:套接字是否可靠,如果我只需要一次/有保证的数据传输? 还有其他解决方案吗? 谢谢。 最佳答案 套接字
引自 this socket tutorial : Sockets come in two primary flavors. An active socket is connected to a
我已经安装了在端口81上运行的流服务器“Lighttpd”(light-tpd)。 我有一个C程序,它使用套接字api创建的服务器套接字在端口80上监听http请求。 我希望从客户端收到端口80上的请
这是我正在尝试做的事情: 当有新消息可用时,服务器会将消息发送给已连接的客户端。另一方面,客户端在连接时尝试使用send()向服务器发送消息,然后使用recv()接收消息,此后,客户端调用close(
如何将消息发送到动态 session 室,以及当服务器收到该消息时,如何将该消息发送到其他成员所在的同一个 session 室? table_id是房间,它将动态设置。 客户: var table_i
这是我尝试但不起作用的方法。我可以将传入的消息从WebSocket连接转发到NetSocket,但是只有NetSocket收到的第一个消息才到达WebSocket后面的客户端。 const WebSo
我正在实现使用boost将xml发送到客户端的服务器。我面临的问题是缓冲区不会立即发送并累积到一个点,然后发送整个内容。这在我的客户端造成了一个问题,当它解析xml时,它可能具有不完整的xml标记(不
尝试使用Nginx代理Gunicorn套接字。 /etc/systemd/system/gunicorn.service文件 [Unit] Description=gunicorn daemon Af
我正在使用Lua套接字和TCP制作像聊天客户端和服务器这样的IRC。我要弄清楚的主要事情是如何使客户端和服务器监听消息并同时发送它们。由于在服务器上执行socket:accept()时,它将暂停程序,
我看了一下ZMQ PUSH/PULL套接字,尽管我非常喜欢简单性(特别是与我现在正在通过UDP套接字在系统中实现的自定义碎片/ack相比),但我还是希望有自定义负载平衡功能,而不是幼稚的回合-robi
我正在编写一个应用程序,其中有多个 socket.io 自定义事件,并且所有工作正常,除了这个: socket.on("incomingImg", function(data) {
在我的应用程序中,我向服务器发送了两条小消息(类似 memcached 的服务)。在类似 Python 的伪代码中,这看起来像: sock.send("add some-key 0") ignored
很抱歉再次发布此问题,但大多数相关帖子都没有回答我的问题。我在使用 socket.io 的多个连接时遇到问题我没有使用“socket.socket.connect”方法,但我从第一次连接中得到了反馈。
我尝试使用 socket.io 客户端连接到非 socket.io websocket 服务器。但我做不到。我正在尝试像这样连接到套接字服务器: var socket = io.connect('ws
我遇到了一个奇怪的问题。在我非常基本的服务器中,我有: server.listen(8001); io.listen(server); var sockets = io.sockets; 不幸的是,套
我正在使用带套接字 io 的sailsjs。帆的版本是 0.10.5。我有以下套接字客户端进行测试: var socketIOClient = require('socket.io-client');
这个问题在这里已经有了答案: What is the fundamental difference between WebSockets and pure TCP? (4 个答案) 关闭 4 年前。
我是一名优秀的程序员,十分优秀!