websocket.nim which I used first started giving weird errors with packets 
merging into each other and SSL failing. Then I switched to ws, but didn't want 
to change my code too much, so I just made a wrapper over both.
    
    
    import ../common # defines websocketBackend
    
    when websocketBackend == "ws":
      import pkg/ws as pkgws
      export pkgws
      
      template isClosed*(ws: WebSocket): bool =
        ws.readyState == ReadyState.Closed
      
      template readData*(ws: WebSocket): untyped =
        ws.receivePacket
      
      proc extractCloseData*(data: string): tuple[code: int, reason: string] =
        ## A way to get the close code and reason out of the data of a Close 
opcode.
        var data = data
        result.code =
          if data.len >= 2:
            (data[0].int shl 8) or data[1].int
          else:
            0
        result.reason = if data.len > 2: data[2..^1] else: ""
    elif websocketBackend == "websocket":
      import websocket, asyncnet
      export websocket except close
      
      type WebSocket* = AsyncWebSocket
      
      template newWebSocket*(args: varargs[untyped]): untyped =
        newAsyncWebsocketClient(args)
      
      template send*(ws: WebSocket, text: string): untyped =
        ws.sendText(text)
      
      template isClosed*(ws: WebSocket): bool =
        bind isClosed
        asyncnet.isClosed(ws.sock)
      
      template close*(ws: WebSocket) =
        discard websocket.close(ws)
    elif websocketBackend == "jswebsockets":
      {.error: "jswebsockets not yet supported".}
    else:
      {.error: "unknown websocket backend " & websocketBackend.}
    
    
    Run

nim-websock looks interesting for the reason that it doesn't use stdlib 
httpclient, but I haven't really needed to try it yet.

> Don't think there a Nim wrapper for it: 
> <https://nng.nanomsg.org/man/v1.3.2/nng.7.html>

There is. <https://github.com/def-/nim-nanomsg>

Reply via email to