Skip to content

Opcode confusion in strict mode #57

Open

Description

@bit-garden

I'm trying to determine when a client connects and disconnects, but I'm not seeing a clear way. I was thinking of adding on some extra opcodes, but I'm seeing an opcode that isn't clearly showing up in the source code.

When a client connects, I get opcode of 16 and a message of the ip address.
When a client disconnects, no message or opcode is being picked up.

Am I missing something or is this not currently a feature? Additionally, where is opcode 16 coming from?

Activity

  1. bit-garden commented on Sep 3, 2025

    @bit-garden
    Author

    I think I figured it out.

    In your examples, you have an early return when the length of the payload is 0.

    Perhaps illustrating the extra op codes or at least how to see if a client connects/disconnects in your examples might help.

  2. allinurl commented on Sep 5, 2025

    @allinurl
    Owner

    Just to clarify your question a bit, are you aiming to handle the onopen, onmessage, and onclose events on your side, or are you specifically referring to using the enum WSOPCODE defined in the API?

  3. bit-garden commented on Sep 5, 2025

    @bit-garden
    Author

    Yes. I was wanting my program to know when a user connects or disconnects. In my case, I would pair the listener with a user login in a database if the user provides a username/password after connecting. If the user disconnects, it would unpair the listener.

    In websocket.c has WSOpcode defined and it has 0, 1, 2, 3, 8, 9 and 10 defined.
    gwsocket.c has ptr += pack_uint32 (ptr, 0x10);. Which is 16, but it's not clear that this code indicates an on connect event.

    typedef enum WSOPCODE {
      WS_OPCODE_CONTINUATION = 0x00,
      WS_OPCODE_TEXT = 0x01,
      WS_OPCODE_BIN = 0x02,
      WS_OPCODE_END = 0x03,
      WS_OPCODE_CLOSE = 0x08,
      WS_OPCODE_PING = 0x09,
      WS_OPCODE_PONG = 0x0A,
    } WSOpcode;

    I found this while trying to figure out why your example codes did not print out disconnect messages initially.

    My full working example is something like this.

    typedef struct {
      uint32_t size;
      uint32_t listener;
      uint32_t type;
      char *msg;
    } Message;
    
    // -----
    
    void on_connect(Message m) {
      printf("user %d connected\n", m.listener);
    }
    
    void on_message(Message m) {
      printf("%d: %s\n", m.listener, m.msg);
    }
    
    void on_disconnect(Message m) {
      printf("user %d disconnected\n", m.listener);
    }
    
    static void read_message(int fd, fd_set set) {
      int bytes = 0;
      uint32_t size = 0, listener = 0, type = 0;
      char hdr[PIPE_BUF] = {0};
      char buf[PIPE_BUF] = {0};
      char *ptr = NULL;
    
      FD_ZERO(&set);
      FD_SET(fd, &set);
    
      if ((select(fd + 1, &set, NULL, NULL, NULL)) < 1)
        exit(1);
      if (!FD_ISSET(fd, &set))
        return;
    
      if (hdr[0] == '\0') {
        if (read(fd, hdr, sizeof(uint32_t) * 3) < 1)
          return;
      }
    
      ptr = hdr;
      ptr += unpack_uint32(ptr, &listener);
      ptr += unpack_uint32(ptr, &type);
      ptr += unpack_uint32(ptr, &size);
    
      read(fd, buf, size);
    
      // if (read(fd, buf, size) < 1) {
      //   return;
      // }
    
      // printf("client: %d, type: %d msg: %s\n", listener, type, buf);
      Message m = {size, listener, type, buf};
      if (type == 16) {
        // connected
        on_connect(m);
      } else if (type == 8) {
        // disconnect
        on_disconnect(m);
      } else if (type == 1) {
        // text
        on_message(m);
      } else if (type == 0) {
        // continue?
      }
    }
  4. allinurl commented on Sep 5, 2025

    @allinurl
    Owner

    I see. It looks like you can simply pass your callback when instantiating the server. For example, here's how the onopen is done in goaccess.

    The server struct provides three options you can use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions