Skip to content

Add Ping/Pong functions #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,29 @@ handleMessage: gets called when there is an incoming message from the client end
- self.data: bytearray (BINARY frame) or unicode string payload (TEXT frame)
- self.request: HTTP details from the WebSocket handshake (refer to BaseHTTPRequestHandler)

handlePing: gets called when there is an incoming PING request from the client endpoint
- self.address: TCP address port tuple of the endpoint
- self.opcode: always PING
- self.data: bytearray payload
- self.request: HTTP details from the WebSocket handshake (refer to BaseHTTPRequestHandler)
- **You must send the PONG reply yourself with sendPong(self.data) [or other data] if you define this function**

handlePong: gets called when there is an incoming PONG reply from the client endpoint
- self.address: TCP address port tuple of the endpoint
- self.opcode: always PONG
- self.data: bytearray payload
- self.request: HTTP details from the WebSocket handshake (refer to BaseHTTPRequestHandler)

sendMessage: send some text or binary data to the client endpoint
- sending data as a unicode object will send a TEXT frame
- sending data as a bytearray object will send a BINARY frame

sendPing: send a PING request to the client endpoint
- send data as a bytearray object

sendPong: send a PONG reply to the client endpoint
- send data as a bytearray object

sendClose: send close frame to endpoint


Expand Down
31 changes: 29 additions & 2 deletions SimpleWebSocketServer/SimpleWebSocketServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ def handleClose(self):
"""
pass

def handlePing(self):
"""
Called when a websocket server gets a Ping frame from a client.
If overridden, make sure to send the PONG reply
"""
self.sendPong(self.data)

def handlePong(self):
"""
Called when a websocket server gets a Pong frame from a client.
"""
pass

def _handlePacket(self):
if self.opcode == CLOSE:
pass
Expand Down Expand Up @@ -229,10 +242,10 @@ def _handlePacket(self):
self.frag_buffer = None

elif self.opcode == PING:
self._sendMessage(False, PONG, self.data)
self.handlePing()

elif self.opcode == PONG:
pass
self.handlePong()

else:
if self.frag_start is True:
Expand Down Expand Up @@ -401,6 +414,20 @@ def sendMessage(self, data):
opcode = TEXT
self._sendMessage(False, opcode, data)

def sendPing(self, data=None):
"""
Send websocket Ping
"""
if not data:
data = bytearray()
self._sendMessage(False, PING, data)

def sendPong(self, data):
"""
Send websocket Pong
"""
self._sendMessage(False, PONG, data)


def _sendMessage(self, fin, opcode, data):

Expand Down