Get the App
SLTechnology News&Howtos  ›  Network Security  › 

40 lines of code chat room

Shulou Source: shulou.com Published: 2022-06-01 07:41:48 10月02日 Update

I saw a great timely communication project today and shared it with you.

Server: lhttp

40 lines of code chat room: demo

Basically, you don't have to write a single line of code on the server side of a chat room, but a very small amount of code on the client side.

It is worth mentioning that the author's idea is very good, the communication protocol is a bit similar to the http protocol, so the scalability is better, the entire server can be easily customized, the author implements and some very important first filters, such as subscription and publication.

If the http protocol is to locate resources, then the greatest use of lhttp is to transmit commands. Is based on the websocket protocol.

What I like most is subscription and publishing, such as:

Send a string like this via websocket:

"LHTTP/1.0 command\ r\ nsubscribe:channel_test\ r\ n\ r\ n"

This means that the link subscribes to the channel_test channel. Suppose another client sends such a string:

"LHTTP/1.0 command\ r\ npublish:channel_test\ r\ n\ r\ nhello"

In this way, all subscribers can receive this message, and the message body is hello.

The command here is very similar to http's url, and the server can register the processor to process this command according to the command. The client can also be handled similarly, such as the command can be chat (chat) enter_group (join the discussion group) add_friend (add friends) and so on, which is very flexible.

Also support some agents and other features, I will not say more, everyone to study and learn together, there is an exchange group: 171358414

Tags: Services subscriptions code commands clients clients processing chat rooms authors characters strings messages communications maximum important worth mentioning one line no representative extensibility Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno macOS MariaDB Xiaomi