Skip to content

Commit

Permalink
Merge pull request #50 from dom96/deprecate-and-unmask
Browse files Browse the repository at this point in the history
Deprecate the AsyncSocket functions and fix bug on Safari.
  • Loading branch information
Araq authored Jan 27, 2019
2 parents e9f79c0 + b174415 commit 62057bf
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions websocket/shared.nim
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,22 @@ proc readData*(ws: AsyncSocket):

return (resultOpcode, resultData)

proc sendText*(ws: AsyncSocket, p: string, maskingKey = generateMaskingKey()): Future[void] {.async.} =
proc sendText*(
ws: AsyncSocket, p: string,
maskingKey = generateMaskingKey()
): Future[void] {.async, deprecated: "Use the AsyncWebSocket variant instead".} =
## Sends text data. Will only return after all data has been sent out.
await ws.send(makeFrame(Opcode.Text, p, maskingKey))

proc sendBinary*(ws: AsyncSocket, p: string, maskingKey = generateMaskingKey()): Future[void] {.async.} =
proc sendBinary*(
ws: AsyncSocket, p: string, maskingKey = generateMaskingKey()
): Future[void] {.async, deprecated: "Use the AsyncWebSocket variant instead".} =
## Sends binary data. Will only return after all data has been sent out.
await ws.send(makeFrame(Opcode.Binary, p, maskingKey))

proc sendPing*(ws: AsyncSocket, maskingKey = generateMaskingKey(), token: string = ""): Future[void] {.async.} =
proc sendPing*(
ws: AsyncSocket, maskingKey = generateMaskingKey(), token: string = ""
): Future[void] {.async, deprecated: "Use the AsyncWebSocket variant instead".} =
## Sends a WS ping message.
## Will generate a suitable token if you do not provide one.

Expand Down Expand Up @@ -327,16 +334,26 @@ proc readData*(ws: AsyncWebSocket): Future[tuple[opcode: Opcode, data: string]]

proc sendText*(ws: AsyncWebSocket, p: string, maskingKey = generateMaskingKey()): Future[void] =
## Sends text data. Will only return after all data has been sent out.
result = sendText(ws.sock, p, maskingKey)
let maskingKey =
if ws.kind == Server: ""
else: maskingKey
result = ws.sock.send(makeFrame(Opcode.Text, p, maskingKey))

proc sendBinary*(ws: AsyncWebSocket, p: string, maskingKey = generateMaskingKey()): Future[void] =
## Sends binary data. Will only return after all data has been sent out.
result = sendBinary(ws.sock, p, maskingKey)
let maskingKey =
if ws.kind == Server: ""
else: maskingKey
result = ws.sock.send(makeFrame(Opcode.Binary, p, maskingKey))

proc sendPing*(ws: AsyncWebSocket, maskingKey = generateMaskingKey(), token: string = ""): Future[void] =
proc sendPing*(ws: AsyncWebSocket, maskingKey = generateMaskingKey(), token: string = "") {.async.} =
## Sends a WS ping message.
## Will generate a suitable token if you do not provide one.
result = sendPing(ws.sock, maskingKey, token)
let maskingKey =
if ws.kind == Server: ""
else: maskingKey
let pingId = if token == "": $genOid() else: token
await ws.sock.send(makeFrame(Opcode.Ping, pingId, maskingKey))

proc closeWebsocket*(ws: AsyncSocket, code = 0, reason = ""): Future[void] {.async.} =
## Closes the socket.
Expand Down

0 comments on commit 62057bf

Please sign in to comment.