Skip to content

Commit 9e06d71

Browse files
committed
added a ping(m) method
1 parent 5da9ef5 commit 9e06d71

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

test/test_websocket.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ def test_closing_message_received(self):
169169
ws.process(b'unused for this test')
170170
c.assert_called_once_with(1000, b'test closing')
171171

172+
def test_sending_ping(self):
173+
tm = PingControlMessage("hello").single(mask=False)
174+
175+
m = MagicMock()
176+
ws = WebSocket(sock=m)
177+
ws.ping("hello")
178+
m.sendall.assert_called_once_with(tm)
179+
172180

173181
if __name__ == '__main__':
174182
suite = unittest.TestSuite()

ws4py/websocket.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from ws4py import WS_KEY, WS_VERSION
99
from ws4py.exc import HandshakeError, StreamClosed
1010
from ws4py.streaming import Stream
11-
from ws4py.messaging import Message, PongControlMessage
11+
from ws4py.messaging import Message, PingControlMessage,\
12+
PongControlMessage
1213
from ws4py.compat import basestring, unicode
1314

1415
DEFAULT_READING_SIZE = 2
@@ -210,6 +211,13 @@ def close_connection(self):
210211
finally:
211212
self.sock = None
212213

214+
def ping(self, message):
215+
"""
216+
Send a ping message to the remote peer.
217+
The given `message` must be a unicode string.
218+
"""
219+
self.send(PingControlMessage(message))
220+
213221
def ponged(self, pong):
214222
"""
215223
Pong message, as a :class:`messaging.PongControlMessage` instance,

0 commit comments

Comments
 (0)