Skip to content

Commit

Permalink
Readd Curl.ws_close()
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfies committed Dec 4, 2024
1 parent b3af027 commit 9cd3c45
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions curl_cffi/curl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import re
import struct
import warnings
from http.cookies import SimpleCookie
from pathlib import Path
Expand Down Expand Up @@ -433,6 +434,23 @@ def ws_send(self, payload: bytes, flags: CurlWsFlag = CurlWsFlag.BINARY) -> int:
self._check_error(ret, "WS_SEND")
return n_sent[0]

def ws_close(self, code: int = 1000, message: bytes = b"") -> int:
"""Close a websocket connection. Shorthand for :meth:`ws_send`
with close code and message. Note that to completely close the connection,
you must close the curl handle after this call with :meth:`close`.
Args:
code: close code.
message: close message.
Returns:
0 if no error.
Raises:
CurlError: if failed.
"""
return self.ws_send(struct.pack("!H", code) + message)


class CurlMime:
"""Wrapper for the ``curl_mime_`` API."""
Expand Down
1 change: 1 addition & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Curl
.. automethod:: close
.. automethod:: ws_recv
.. automethod:: ws_send
.. automethod:: ws_close

AsyncCurl
~~~~~~
Expand Down

0 comments on commit 9cd3c45

Please sign in to comment.