-
Notifications
You must be signed in to change notification settings - Fork 1
Publisher Subscriber
Jiowcl edited this page May 20, 2021
·
5 revisions
EnableExplicit
IncludeFile "../Core/ZeroMQ.pbi"
Global lpszLibZmqDll.s = "libzmq.dll"
Global lpszServerAddr.s = "tcp://*:1689"
Global hLibrary.i = ZmqDllOpen(lpszLibZmqDll)
If hLibrary
OpenConsole()
Define Context.i = ZmqCtxNew(hLibrary)
Define Socket.i = ZmqSocket(hLibrary, Context, #ZMQ_PUB)
Define Rc.i = ZmqBind(hLibrary, Socket, lpszServerAddr)
PrintN("Bind an IP address: " + lpszServerAddr)
While 1
Define *lpszBuffer = AllocateMemory(32)
Define lpszTopic.s = "quotes"
Define lpszMessage.s = "Bid:" + Random(9000, 1000) + ",Ask:" + Random(9000, 1000)
ZmqRecv(hLibrary, Socket, *lpszBuffer, MemorySize(*lpszBuffer), 0)
Delay(10)
Define lpszReturnMessage.s = PeekS(*lpszBuffer, -1, #PB_UTF8)
If lpszReturnMessage <> ""
PrintN("Received: ")
PrintN(lpszReturnMessage)
EndIf
ZmqSend(hLibrary, Socket, lpszTopic, Len(lpszTopic), #ZMQ_SNDMORE)
ZmqSend(hLibrary, Socket, lpszMessage, Len(lpszMessage), 0)
FreeMemory(*lpszBuffer)
Wend
ZmqClose(hLibrary, Socket)
ZmqCtxShutdown(hLibrary, Context)
CloseConsole()
ZmqDllClose(hLibrary)
EndIf
EnableExplicit
IncludeFile "../Core/ZeroMQ.pbi"
Global lpszLibZmqDll.s = "libzmq.dll"
Global lpszServerAddr.s = "tcp://localhost:1689"
Global hLibrary.i = ZmqDllOpen(lpszLibZmqDll)
If hLibrary
OpenConsole()
Define Context.i = ZmqCtxNew(hLibrary)
Define Socket.i = ZmqSocket(hLibrary, Context, #ZMQ_SUB)
Define Rc.i = ZmqConnect(hLibrary, Socket, lpszServerAddr)
Define lpszSubscribe.s = "quotes"
ZmqSetsockopt(hLibrary, Socket, #ZMQ_SUBSCRIBE, lpszSubscribe, Len(lpszSubscribe))
Define i.i
For i = 0 To 10
Define *lpszTopicBuffer = AllocateMemory(32)
Define *lpszBuffer = AllocateMemory(32)
ZmqRecv(hLibrary, Socket, *lpszTopicBuffer, MemorySize(*lpszTopicBuffer), 0)
ZmqRecv(hLibrary, Socket, *lpszBuffer, MemorySize(*lpszBuffer), 0)
PrintN( PeekS(*lpszBuffer, -1, #PB_UTF8) )
FreeMemory(*lpszTopicBuffer)
FreeMemory(*lpszBuffer)
Next
ZmqClose(hLibrary, Socket)
ZmqCtxShutdown(hLibrary, Context)
Input()
CloseConsole()
ZmqDllClose(hLibrary)
EndIf