def __init__(self, remote_addr, protocol, extra=None): self._proto = protocol self._web_sock = websocket.WebSocket(remote_addr) self._web_sock.bind('close', lambda evt: protocol.connection_lost()) self._web_sock.bind('open', lambda evt: protocol.connection_made(self)) self._web_sock.bind('message', lambda evt: protocol.datagram_received(evt.data))
def attempt(): console.log('connecting...') self.socket = websocket.WebSocket(WS_URL) self.socket.bind('message', on_message) self.socket.bind('open', on_open) self.socket.bind('close', wait_retry) self.socket.bind('error', lambda event: console.log(event))
def connect(ws_url=None): global WS_URL if ws_url: WS_URL = ws_url project_id = document.select('.lp-viewer')[0].id ws = websocket.WebSocket(WS_URL + '/projects/' + project_id) ws_bind(ws)
def ws_error(): if 'ws_server' not in storage or storage['ws_server'] not in document['websocket_ip'].value: if cache['ips_pos']+1 <= len(cache['ips']): global ws cache['ips_pos'] = cache['ips_pos']+1 ip = cache['ips'][cache['ips_pos']] address = 'ws://'+ip+":9789/websocket" ws = websocket.WebSocket(address)
def connect(ws_url=None): global WS_URL if ws_url: WS_URL = ws_url print("WS_URL", WS_URL) camera_id = document.select(".get-id")[0].id # print(camera_id) ws = websocket.WebSocket(WS_URL + "/cameras/" + camera_id) ws_bind(ws)
def _open(ev): if not websocket.supported: alert("WebSocket is not supported by your browser") return global ws # open a web socket ws = websocket.WebSocket("wss://echo.websocket.org") # bind functions to web socket events ws.bind('open', on_open) ws.bind('message', on_message) ws.bind('close', on_close)
def _open(ev=None): if not websocket.supported: alert("WebSockets not supported...") return global ws l = window.location server_uri = "wss://" if l.protocol == "https:" else "ws://" + \ l.hostname + ":" + l.port + "/ws" ws = websocket.WebSocket(server_uri) ws.bind('open', on_open) ws.bind('message', on_message) ws.bind('close', on_close) ws.bind('error', on_error)
def openSocket(self): if not websocket.supported: alert("WebSocket is not supported by your browser") return if self.state == eDisconnected: self.state = eConnecting # open a web socket self.webSocket = websocket.WebSocket(self.url) # bind functions to web socket events self.webSocket.bind('open', self.on_open) self.webSocket.bind('close', self.on_close) self.webSocket.bind('message', self.on_message) self.webSocket.bind('error', self.on_wsError)
def _open(ev): if not __BRYTHON__.has_websocket: alert("WebSocket is not supported by your browser") return global ws # open a web socket ws = websocket.WebSocket("ws://localhost:8765") ws.bind('open', on_open) ws.bind('message', on_message) ws.bind('close', on_close) ws.bind('error', on_error)
def __init__(cls, ip): """""" if not websocket.supported: print("WebSocket is not supported by your browser") return cls.ip_ = ip # open a web socket # cls.ws = websocket.WebSocket("wss://192.168.1.20:8888") cls.ws = websocket.WebSocket(ip) # bind functions to web socket events cls.ws.bind('open', cls.on_open) cls.ws.bind('error', cls.on_error) cls.ws.bind('message', cls.on_message) cls.ws.bind('close', cls.on_close) port_ip = ip.replace('wss://', '').replace('ws://', '').replace('/ws', '') # cls.ip = port_ip[:port_ip.find(":")] # cls.port = port_ip[port_ip.find(":")+1:] cls.ip = port_ip cls.protocol = 'wss' if 'wss' in ip else 'ws'
add_log("界面启动") if 'ws_server' in storage and storage['ws_server'] in document[ 'websocket_ip'].value: ip = storage['ws_server'] add_log('连接至预存的' + storage['ws_server']) else: ips = document['websocket_ip'].value.split('|') cache['ips'] = ips cache['ips_pos'] = 0 ip = ips[0] address = 'ws://' + ip + ":9789/websocket" ws = websocket.WebSocket(address) def event_log(_msg): add_log('[' + _msg['_account_'] + '] ' + _msg['log']) def event_product(_msg): _doc = document['marketdata'] _dict = _msg['data'] storage['product_list'] = json.dumps(_dict.keys()) add_log(str(_dict)) DirectionDict = {"0": "买", "1": "卖"} DirectionStyle = {
from browser import websocket, window, document def on_open(event): print('connected') def on_message(event): print('connected') document['message'].textContent = event.data ws = websocket.WebSocket(f'ws://{window.location.host}/ws/push') ws.bind('open', on_open) ws.bind('message', on_message)
def get_socket(cls, url, new=False): if url not in cls.SOCKETS: cls.SOCKETS[url] = websocket.WebSocket(url) cls.SOCKETS[url].bind('close', cls.close_handler(url)) return cls.SOCKETS[url]
from browser import document, alert, websocket, window from browser.session_storage import storage from browser.html import DIV import datetime if 'chat-nickname' not in storage.keys(): window.location.href = 'http://localhost/python_chat/' send_button = document['send-button'] txtarea = document['txtarea'] ws = websocket.WebSocket("ws://192.168.1.24:443") nickname = storage['chat-nickname'] def append_to_chat_area(data, is_mine=False): chat_area = document['chat-area'] time_as_str = str(datetime.datetime.now().time()) separator = " | " if not is_mine: main_msg_css_class = 'text-left alert alert-primary' message = time_as_str + separator + data else: main_msg_css_class = 'text-right alert alert-dark' message = data + separator + time_as_str chat_area <= DIV(message, Class=main_msg_css_class) send_button.scrollIntoView() def on_click_on_send_button(ev): ev.preventDefault() message = txtarea.value
def on_open(evt): ws.send(f'{user} Entrouuuu na salala') @bind('#send', 'click') def on_enviar(evt): message = document["text_message"].value if len(message) > 1 or "\n" not in message: ws.send(f'{user} disse: {message}') document["text_message"].value = "" else: document["text_message"].value = "" @bind('#text_message', 'keydown') def on_lala(evt): if evt.which == 13: on_enviar(evt) user = prompt('Digite seu nome para o chat') if window.location.protocol == "https:": ws_scheme = "wss://" else: ws_scheme = "ws://" ws = websocket.WebSocket(f'{ws_scheme}{window.location.host}/ws/duplex/{user}') ws.bind('message', on_message) ws.bind('open', on_open)
from browser import prompt, websocket, window, document, html, bind name = prompt('Enter your name ') ws = websocket.WebSocket(f'ws://{window.location.host}/ws/full-duplex/{name}') @bind('#send', 'click') def send(event): ws.send(f'{name} disse: {document["text"].value}') document["text"].value = '' def on_open(event): ws.send(f'{name}, Entrou na sala.') def on_message(event): messages = document['messages'] messages <= html.P(event.data) messages.scrollTop = messages.scrollHeight - messages.clientHeight ws.bind('open', on_open) ws.bind('message', on_message)