A simple .NET chat client that connects to a server on TCP port 10000.
The project is organized into different forders:
Consts: contains classes of constantsEnums: contains classes of enumerationsExceptions: constains custom exceptionsModels: contains model classes, such as ChatMessage or UserServices: contains service classes injected as dependencies by LightInject libraryChatForm.cs: main chat UI formProgram.cs: app launcher, contains the injection of services
The client allows user to choose a chat name and connect to the server using that name. After connection, the user can send simple text messages and the server broadcast them to all connected users. When user wants to leave the chat, he can disconnect or simply close che UI form.
To connect to the server, the client opens a TCP connection on port 10000.
If the connection opens, it creates a thread timer that runs a method every 500 milliseconds to check if there are new messages to receive.
Network connection and interactions with server is managed by the TcpNetworkService, that is injected when the client starts.
I defined a simple chat protocol to identify different kind of messages, two in this case.
- Connection message: is the one sent when a new user is joining the chat
- Standard message: the type that represents every chat message sent by connected users
Connection messages are identified by the prefix conn: while the chat messages simply by the prefix msg:
Every message consists of prefix:user|message.
In this way the server can properly reply to clients and decide to broadcast a message to all connected users or just notify something to the sender.
A simple Node.js server that listen on TCP port 10000 using net module. It's available at https://gist.github.com/danieleardissone/02e4a5481c638c29c818ae9137317134
