A simple chat-server implemented using raw tcp sockets. This is a working prototype/POC for experimental purposes.
It reuses the same socket that was created during the clients registration throughout a single session hence providing real-time pushes from the server.
Just do go build
and launch the compiled binary.
You will need at least 2 TCP clients, REST clients will not work and they add a lot of header information that the server is not expecting.
- Start the server. It will start up at port 8000.
- Register the clients. Registration process is very trivial and explained below.
- Send a TCP request to the server with a single line of plain text(used for client name) as payload.
- The server reads a single line from the request and uses this string as a name for the new client.
- Server creates a new client with a passed name. Registration is now complete.
- After creating the client, the server does NOT close the socket. Instead, it starts listening over the same socket for messages.
- Send message to other client(s).
- After registration, clients can exchange messages. A message has the following JSON format:
{ "Recipients": ["client1", "client2"...], "Content": "Hi, I'm a cow", }
- A JSON encoded single-line(uglified) string represents a single message, the server considers each line to an individual message.
Recipients
specify the recipients of this message as expected andContent
is the message content.- To send a message, just write a message string(in the above format) into the already existing socket(no new connection required). This can be throughout a single chat session.