I have a TCP server written in SwiftNIO
, based on this documentation.
I want my client to be written in python
from which I can send multiple JSON
strings & can receive similar/different multiple JSON
string(s) as a response periodically for a few minutes.
In which format do I need to convert those JSON
strings from the python
client & how do I get the same JSON
string on the SwiftNIO
server (and vice versa)?
If I were you, I'd use HTTP using the Vapor web server and any Python HTTP library such as
requests
. If you do that, then your job will be pretty straightforward. The Vapor community is also super helpful in their Discord chat.If you really want to do this in a low-level library like SwiftNIO then that's of course possible but you'll need to design a so called "wire protocol" for the framing (ie. when does one JSON message start and end). SwiftNIO is very well equipped for these things but you'll likely need to learn a bunch of things.
You could for example use NIO Extras'
LineBasedFrameDecoder
and send each JSON (make sure it doesn't contain newlines) followed by a\n
. Or you could say that you prepend the JSON by say a 32 bit length field (which you could decode using theLengthFieldBasedFrameDecoder
. There are many options...You could also implement JSON-RPC and you could get some inside in this example which is also explained in this talk.