Rewrite as websockets
Anirudh Oppiliappan x@icyphox.sh
Fri, 17 Jul 2020 18:09:55 +0530
4 files changed,
26 insertions(+),
77 deletions(-)
M
src/wsabi.nim
→
src/wsabi.nim
@@ -1,79 +1,32 @@
-import asyncnet, asyncdispatch -import wsabipkg/utils +import ws, asyncdispatch, asynchttpserver -type - Server = ref object - socket: AsyncSocket - clients: seq[Client] +# Global vars +var + server = newAsyncHttpServer() + remote: WebSocket - Client = ref object - socket: AsyncSocket - netAddr: string - id: int - connected: bool +proc connectRemote(host: string) {.async.} = + remote = await newWebSocket(host) + echo "connected to remote" -proc `$`(client: Client): string = - $client.id & "(" & client.netAddr & ")" +proc sendRemote(ws: WebSocket, data: string) {.async.} = + await ws.send(data) + echo await ws.receiveStrPacket() -proc newServer(): Server = - Server(socket: newAsyncSocket(), clients: @[]) +proc localServer(req: Request) {.async.} = + var + ws = await newWebSocket(req) -proc sendRemote(remote: AsyncSocket, line: string) {.async.} = - await remote.send(line & "\c\L") - let msg = await remote.recv(10000) - if msg.len == 0: - echo("remote closed connection") - remote.close() - quit 0 - else: - echo msg + await connectRemote("ws://test.ws-benchmark.cometchat-dev.com:7070/ws/") + try: + while ws.readyState == Open: + let packet = await ws.receiveStrPacket() + echo "local msg: " & packet + await sendRemote(remote, packet) + except WebSocketError: + echo "client closed socket" -proc processMsgs( - server: Server, client: Client, - remote: AsyncSocket - ) {.async.} = - while true: - let line = await client.socket.recvLine() - if line.len == 0: - echo(client, " disconnected!") - client.connected = false - - client.socket.close() - return - - echo(client, "sent: ", line) - asyncCheck sendRemote(remote, line) +when isMainModule: + waitFor server.serve(Port(9001), localServer) -proc loop(server: Server, port = 6000) {.async.} = - server.socket.bindAddr(port.Port) - echo("server initialised!") - server.socket.listen() - echo("listening on localhost:", port) - let remote = newAsyncSocket() - await remote.connect("example.com", 80.Port) - echo("connecting to remote... ") - echo("connected to remote:", getPeerAddr(remote)) - - while true: - let (netAddr, clientSocket) = await server.socket.acceptAddr() - echo("accepted connection from ", netAddr) - - let client = Client( - socket: clientSocket, - netAddr: netAddr, - id: server.clients.len, - connected: true - ) - server.clients.add(client) - asyncCheck processMsgs(server, client, remote) - -when isMainModule: - setControlCHook(handleCtrlC) - var server = newServer() - try: - waitFor loop(server) - except EKeyboardInterrupt: - echo("interrupted! closing sockets...") - server.socket.close() - quit 0
D
src/wsabipkg/utils.nim
@@ -1,6 +0,0 @@
-type - EKeyboardInterrupt* = object of Exception - -proc handleCtrlC*() {.noconv.} = - raise newException(EKeyboardInterrupt, "Keyboard Interrupt") -
M
wsabi.nimble
→
wsabi.nimble
@@ -12,3 +12,4 @@
# Dependencies requires "nim >= 1.0" +requires "ws >= 0.4.1"