Skip to content

Commit

Permalink
Attempt to improve the ws connection
Browse files Browse the repository at this point in the history
  • Loading branch information
olijeffers0n committed Jan 8, 2022
1 parent 6479ba6 commit f9ee1cd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rustplus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@

__name__ = "rustplus"
__author__ = "olijefferson"
__version__ = "5.1.9"
__version__ = "5.1.10"
__support__ = "Discord: https://discord.gg/nQqJe8qvP8"
1 change: 1 addition & 0 deletions rustplus/api/remote/rust_remote_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def close(self) -> None:

if self.ws is not None:
self.ws.close()
del self.ws
self.ws = None
self.open = False

Expand Down
40 changes: 40 additions & 0 deletions rustplus/api/remote/rustws.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import asyncio
import logging
import time
import socket
import errno
from typing import Optional
from ws4py import exc
from ws4py.client.threadedclient import WebSocketClient
from ws4py.websocket import pyOpenSSLError

from .rustplus_pb2 import AppMessage, AppRequest
from ..structures import RustChatMessage
Expand Down Expand Up @@ -184,3 +188,39 @@ async def get_response(self, seq : int, app_request : AppRequest, retry_depth :
raise RequestError(response.response.error.error)

return response

def once(self):

if self.terminated:
logging.getLogger("ws4py").debug("WebSocket is already terminated")
return False
try:
b = b''
if self._is_secure:
b = self._get_from_pending()
if not b and not self.buf:
try:
b = self.sock.recv(self.reading_buffer_size)
except OSError:
self.connect()
return True
if not b and not self.buf:
return False
self.buf += b
except (socket.error, OSError, pyOpenSSLError) as e:
if hasattr(e, "errno") and e.errno == errno.EINTR:
pass
else:
self.unhandled_error(e)
return False
else:
# process as much as we can
# the process will stop either if there is no buffer left
# or if the stream is closed
# only pass the requested number of bytes, leave the rest in the buffer
requested = self.reading_buffer_size
if not self.process(self.buf[:requested]):
return False
self.buf = self.buf[requested:]

return True

0 comments on commit f9ee1cd

Please sign in to comment.