File: client.py

package info (click to toggle)
python-websocketd 0.5-1
  • links: PTS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 368 kB
  • sloc: python: 1,309; javascript: 382; sh: 23; makefile: 11
file content (40 lines) | stat: -rwxr-xr-x 904 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/python3

import fhs
import time
import websocketd

fhs.option('port', 'port to connect to', default = '8888')
config = fhs.init(help = 'testing program for websocketd module', version = '0.1', contact = 'Bas Wijnen <wijnen@debian.org>')

class Client:
	def __init__(self, remote):
		self.remote = remote
		self.remote._websocket_closed = self._closed

	def func(self, *args, **kwargs):
		print('func called with args %s and %s' % (repr(args), repr(kwargs)))
		return 'func return'

	def ping(self, arg):
		wake = (yield)
		print('ping', arg)

		now = time.time()
		websocketd.add_timeout(now + 1, wake)
		yield

		self.remote.pong.event(arg)

		websocketd.add_timeout(now + 2, wake)
		yield

		return 'pong %s' % repr(arg)

	def _closed(self):
		print('Server disconnected from client')
		websocketd.endloop()

client = websocketd.RPC(config['port'], Client, tls = False)

websocketd.fgloop()