A crude, shaming version of vue, Netty’s Websocket-based instant messaging system. Making: github.com/wangkeshan9… Site: http://47.105.88.240:8080/

Use the sample

  • !!!!!!!!! If it is a PC browser, it is recommended to use Chrome, then open F12, then CTRL + Shift + M to enter mobile device simulation, otherwise right swipe to add friends will not complete!!

The main structure

The packet structure is in JSON form:

    private String command;

    private String traceId;

    private Object data;
Copy the code

Command indicates the type of packet instruction. Websocket protocol is full-duplex and cannot be in the form of q&a like HTTP. Therefore, for clients, a request and a response cannot be linked in the native protocol, so traceId is used to determine. The data for the data

Front-end structure Description

If the response mode requires a traceId, the REQ and the corresponding callback method are queued. If the response mode is resP, the callback process is one-way. If the response mode is resP, the registered method is found according to the command

The back-end structure

handler:

        pipeline.addLast(new IMIdleStateHandler()); // Idle connection processing, timeout closed
        pipeline.addLast(new HttpServerCodec());// HTTP codec
        pipeline.addLast(new HttpObjectAggregator(65536)); // Aggregate the chunk content in HTP Requet,
        pipeline.addLast(new ChunkedWriteHandler());// Aggregate a large amount of data in response
        pipeline.addLast(new WebSocketServerCompressionHandler());// Handle websocket extensions and determine protocol upgrades
        pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null.true));// The WebSocket handshake handler is automatically added, and the WebSocket codec is added after the handshake
        pipeline.addLast(new HttpStaticFileServerHandler());// Static page service,
        pipeline.addLast(new WebSocketFrameHandler()); // Start of business process
Copy the code

Examples of business methods:

@Command(ADD_FRIENDS)
public class AddFriendsService extends BaseService<AddFriendReq.AddFriendResp> {

    @Override
    AddFriendResp process(MsgContext context, AddFriendReq addFriendReq) {
        UserService.addFriend(addFriendReq.getUserId(), addFriendReq.getAddId());

        // The response to be added
        UserInfo userInfo = UserService.getUserInfo(addFriendReq.getUserId());
        Packet p = new Packet(ADD_NOTIRY, null.new AddFriendNotify(userInfo.getUserId(), userInfo.getUserName()));
        UserInfo addedUserInfo = UserService.getUserInfo(addFriendReq.getAddId());
        send(addedUserInfo.getChannel(), p);

        return new AddFriendResp("SUCCESS"."SUCCESS", addedUserInfo.getUserId(), addedUserInfo.getUserName()); }}Copy the code

@command is a custom annotation, the Command type for annotation processing, the generic type is REq, resP type,

run

Front-end: Vue UI directly packaged back-end: Java-jar-dfront_dir =E:\code\Netty_IM\front\im_ui\dist -Dport=8080 netty_im-1.0.jar front_dir is the front-end directory port

TODO

In fact, it looks like a completely crude toy, so make a list of directions that I can continue to do, although I’m too lazy to do it

  1. Netty is still a little too low-level to use, static server code to find their own, so you can consider switching to VerTX, relatively small and fresh, but also can do clustering
  2. Wanted to make a pressure tool, to pressure the performance,
  3. SSL support,