-
Notifications
You must be signed in to change notification settings - Fork 0
Publisher Subscriber
Jiowcl edited this page Dec 14, 2024
·
1 revision
EnableExplicit
IncludeFile "../Core/Nanomsg.pbi"
Global lpszCurrentDir.s = GetCurrentDirectory()
; Nanomsg version (x64)
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
Global lpszLibNnDir.s = "Library/x64"
Global lpszLibNnDll.s = lpszCurrentDir + lpszLibNnDir + "/nanomsg.dll"
SetCurrentDirectory(lpszCurrentDir + lpszLibNnDir)
CompilerEndIf
Global lpszServerAddr.s = "tcp://*:1689"
Global hLibrary.i = NnDllOpen(lpszLibNnDll)
If hLibrary
OpenConsole()
Define Socket.i = NnSocket(hLibrary, #AF_SP, #NN_PUB)
Define Rc.i = NnBind(hLibrary, Socket, lpszServerAddr)
PrintN("Bind an IP address: " + lpszServerAddr)
While 1
Define *lpszBuffer = AllocateMemory(32)
Define lpszTopic.s = "quotes"
Define lpszMessage.s = lpszTopic + "#Bid:" + Random(9000, 1000) + ",Ask:" + Random(9000, 1000)
NnRecv(hLibrary, Socket, *lpszBuffer, MemorySize(*lpszBuffer), 0)
Delay(10)
Define lpszReturnMessage.s = PeekS(*lpszBuffer, -1, #PB_UTF8)
If lpszReturnMessage <> ""
PrintN("Received: ")
PrintN(lpszReturnMessage)
EndIf
NnSend(hLibrary, Socket, lpszMessage, Len(lpszMessage), 0)
FreeMemory(*lpszBuffer)
Wend
NnClose(hLibrary, Socket)
CloseConsole()
NnDllClose(hLibrary)
EndIf
EnableExplicit
IncludeFile "../Core/Nanomsg.pbi"
Global lpszCurrentDir.s = GetCurrentDirectory()
; Nanomsg version (x64)
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
Global lpszLibNnDir.s = "Library/x64"
Global lpszLibNnDll.s = lpszCurrentDir + lpszLibNnDir + "/nanomsg.dll"
SetCurrentDirectory(lpszCurrentDir + lpszLibNnDir)
CompilerEndIf
Global lpszServerAddr.s = "tcp://localhost:1689"
Global hLibrary.i = NnDllOpen(lpszLibNnDll)
If hLibrary
OpenConsole()
Define Socket.i = NnSocket(hLibrary, #AF_SP, #NN_SUB)
Define Rc.i = NnConnect(hLibrary, Socket, lpszServerAddr)
Define lpszSubscribe.s = "quotes"
NnSetsockopt(hLibrary, Socket, #NN_SUB, #NN_SUB_SUBSCRIBE, lpszSubscribe, Len(lpszSubscribe))
While 1
Define *lpszBuffer = AllocateMemory(32)
NnRecv(hLibrary, Socket, *lpszBuffer, MemorySize(*lpszBuffer), 0)
PrintN( PeekS(*lpszBuffer, -1, #PB_UTF8) )
FreeMemory(*lpszBuffer)
Delay(10)
Wend
NnClose(hLibrary, Socket)
Input()
CloseConsole()
NnDllClose(hLibrary)
EndIf