├── tests ├── test1.nim └── config.nims ├── src ├── websocketx.nim └── websocketx │ └── httpx.nim ├── websocketx.nimble ├── examples └── simple.nim ├── LICENSE └── README.md /tests/test1.nim: -------------------------------------------------------------------------------- 1 | import websocketx 2 | -------------------------------------------------------------------------------- /tests/config.nims: -------------------------------------------------------------------------------- 1 | switch("path", "$projectDir/../src") -------------------------------------------------------------------------------- /src/websocketx.nim: -------------------------------------------------------------------------------- 1 | import ./websocketx/httpx 2 | import pkg/ws 3 | 4 | export httpx, ws 5 | -------------------------------------------------------------------------------- /websocketx.nimble: -------------------------------------------------------------------------------- 1 | # Package 2 | 3 | version = "0.1.2" 4 | author = "flywind" 5 | description = "Websocket for httpx." 6 | license = "MIT" 7 | srcDir = "src" 8 | 9 | 10 | 11 | # Dependencies 12 | 13 | requires "nim >= 1.2.6" 14 | requires "ws >= 0.4.3" 15 | requires "httpx >= 0.1.8" 16 | -------------------------------------------------------------------------------- /examples/simple.nim: -------------------------------------------------------------------------------- 1 | import std/[options, asyncdispatch] 2 | 3 | import ../src/websocketx 4 | 5 | import pkg/httpx 6 | 7 | proc onRequest(req: Request) {.async.} = 8 | if req.path.isSome: 9 | if req.path.get == "/ws": 10 | var ws = await newWebSocket(req) 11 | await ws.send("Welcome to simple echo server") 12 | while ws.readyState == Open: 13 | let packet = await ws.receiveStrPacket() 14 | await ws.send(packet) 15 | else: 16 | req.send(Http404) 17 | else: 18 | req.send(Http404) 19 | 20 | 21 | run(onRequest) 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 flywind 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/websocketx/httpx.nim: -------------------------------------------------------------------------------- 1 | # MIT 2 | # Copyright 2018 Andre von Houck 3 | 4 | # MIT 5 | # Copyright 2020 xflywind 6 | 7 | 8 | import std/[asyncdispatch, asyncnet, options] 9 | import pkg/[ws, httpx] 10 | 11 | 12 | proc newWebSocket*(req: Request): Future[WebSocket] {.async.} = 13 | ## Creates a new socket from an httpbeast request. 14 | try: 15 | let headers = req.headers.get 16 | 17 | if not headers.hasKey("Sec-WebSocket-Version"): 18 | req.send(Http404, "Not Found") 19 | raise newException(WebSocketError, "Not a valid websocket handshake.") 20 | 21 | var ws = WebSocket() 22 | ws.masked = false 23 | 24 | # Here is the magic: 25 | req.forget() # Remove from HttpBeast event loop. 26 | asyncdispatch.register(req.client.AsyncFD) # Add to async event loop. 27 | 28 | ws.tcpSocket = newAsyncSocket(req.client.AsyncFD) 29 | await ws.handshake(headers) 30 | return ws 31 | 32 | except ValueError, KeyError: 33 | # Wrap all exceptions in a WebSocketError so its easy to catch 34 | raise newException( 35 | WebSocketError, 36 | "Failed to create WebSocket from request: " & getCurrentExceptionMsg() 37 | ) 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # websocketx 2 | Nim websocket for httpx. 3 | 4 | Based on https://github.com/treeform/ws 5 | 6 | ## Installation 7 | ``` 8 | nimble install websocketx 9 | ``` 10 | 11 | ## Usage 12 | 13 | ### httpx 14 | 15 | ```nim 16 | import options, asyncdispatch, httpx, websocketx 17 | 18 | 19 | proc onRequest(req: Request) {.async.} = 20 | if req.path.isSome: 21 | if req.path.get == "/ws": 22 | var ws = await newWebSocket(req) 23 | await ws.send("Welcome to simple echo server") 24 | while ws.readyState == Open: 25 | let packet = await ws.receiveStrPacket() 26 | await ws.send(packet) 27 | else: 28 | req.send(Http404) 29 | else: 30 | req.send(Http404) 31 | 32 | 33 | run(onRequest) 34 | ``` 35 | 36 | ### asyncdispatch 37 | 38 | ```nim 39 | import websocketx, asyncdispatch, asynchttpserver 40 | 41 | var server = newAsyncHttpServer() 42 | proc cb(req: Request) {.async.} = 43 | if req.url.path == "/ws": 44 | var ws = await newWebSocket(req) 45 | await ws.send("Welcome to simple echo server") 46 | while ws.readyState == Open: 47 | let packet = await ws.receiveStrPacket() 48 | await ws.send(packet) 49 | else: 50 | await req.respond(Http404, "Not found") 51 | 52 | waitFor server.serve(Port(9001), cb) 53 | ``` 54 | --------------------------------------------------------------------------------