Skip to content
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

I need a help #99

Open
jorgebg2016 opened this issue Sep 3, 2020 · 1 comment
Open

I need a help #99

jorgebg2016 opened this issue Sep 3, 2020 · 1 comment

Comments

@jorgebg2016
Copy link

jorgebg2016 commented Sep 3, 2020

I would like to know whether is possible to call a function outside the server class handler. I have to emit datas from my Python modules for the Javascript events handler. Somebody can help me?

@ccostel
Copy link

ccostel commented Oct 12, 2020

Not sure I understand your question. But I think this example that I also stole from somewhere here :)

import threading
import time
import json
import logging
import websockets
from queue_helpers import deque


from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket

USERS = set()

class SimpleEcho(WebSocket):
	def handleMessage(self):
		self.sendMessage(self.data)

	def handleConnected(self):
		USERS.add(self)

	def handleClose(self):
		USERS.remove(self)

server = SimpleWebSocketServer('', 8888, SimpleEcho)
# server = SimpleWebSocketServer('', 8888, SimpleEcho, selectInterval=0.1)

def push_out_news():
	while True:
		news_item = deque()
		if USERS and news_item:
			for user in USERS:
				user.sendMessage(str(news_item))
		time.sleep(0.2)

if __name__ == '__main__':
	t = threading.Thread(target=server.serveforever)
	t.setDaemon(True)
	t.start()
	push_out_news()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants