Listening for connect/connection events within ZMQ request-reply pattern

时间:2015-05-12 23:15:30

标签: node.js zeromq

In the replier or 'server' of the ZMQ request-reply pattern, I would like to listen to requestors connecting to my replier/server.

I have this code:

.model small
.stack 0100h
.data
a db 0ah, 0dh,"ENTER FIRSTNAME: $"
b db 0ah, 0dh,"ENTER SECONDNAME: $" 
c db 20d, ?, 20d DUP("$")
d db 20d, ?, 20d DUP("$")           
crlf db 13,10,'$'
.code
main proc
mov ax, @data
mov ds, ax
mov es, ax
call cls
call input       
call finish_strings   ;<========================================
call output

mov ax, 4c00h
int 21h
main endp

;clear screen
cls proc
mov ax, 0600h
mov bh, 07h
mov cx, 0000h
mov dx, 184fh
int 10h
ret
cls endp

;INPUT
input proc
mov ah, 09h
mov dx, OFFSET a
int 21h
mov ah, 0ah
mov dx, OFFSET c
int 21h
mov ah, 09h
mov dx, OFFSET b
int 21h
mov ah, 0ah
mov dx, OFFSET d
int 21h        
ret
input endp         


;FINISH STRINGS.
finish_strings proc   ;<========================================

;FINISH STRING C.

mov  si, offset c+1
mov  al, [ si ]
mov  ah, 0
add  si, ax
inc  si
mov  al, '$'
mov  [ si ], al

;FINISH STRING D.

mov  si, offset d+1       ;STRING LENGTH.
mov  al, [ si ]           ;AL = STRING LENGTH.
mov  ah, 0                ;CLEAR AH TO USE AX.
add  si, ax               ;USE LENGTH AS OFFSET.
inc  si                   ;LENGTH + 1.
mov  al, '$'
mov  [ si ], al           ;CHANGE 13 BY '$'.

ret    
finish_strings endp


;OUTPUT
output proc

mov  ah, 9
lea  dx, crlf
int  21h

mov ah, 09h
mov dx, OFFSET c+2
int 21h
mov ah, 02h
mov dl, 003h
int 21h
mov ah, 09h
mov dx, OFFSET d+2
int 21h
ret
output endp

end main

but my requestor is indeed connecting and sending messages to my replier, like so:

    var zmqConfig = {...};
    var replier = zmq.socket('rep');

    var address = 'tcp://'.concat(zmqConfig.host).concat(':').concat(zmqConfig.port);

    replier.bind(address, function (err) {
        if (err) {

        }
    });

        replier.on('message', function () {
         // this is firing
         });

        replier.on('connect',function(){
         // but this is NOT firing
       });

       replier.on('connection',function(){
        // neither is this
       });

1 个答案:

答案 0 :(得分:1)

“connect”事件在连接的一侧触发,这不是你在这里寻找的东西。你想要的是“accept”事件,当绑定套接字接受来自对等体的新连接时会触发该事件。

要捕获此事件,您必须在连接发生之前调用套接字上的monitor()方法...可能在您的套接字bind()之前。您放入monitor()方法的计时器不会影响它触发的事件,只会触发它们。

以下是您的代码修改后以这种方式工作:

var zmqConfig = {...};
var replier = zmq.socket('rep');
replier.monitor(50); // just picked a time

var address = 'tcp://'.concat(zmqConfig.host).concat(':').concat(zmqConfig.port);

replier.bind(address, function (err) {
    if (err) {

    }
});

replier.on('message', function () {
    // this is firing
});

replier.on('accept', function(){
    // this should *now* fire when you accept a connection from a peer
});