Untitled

From lovetox, 1 Month ago, written in Plain Text, viewed 15 times. This paste will kick the bucket in 1 Second.
URL https://paste.gajim.org/view/1cdb1609 Embed
Download Paste or View Raw
  1. from gi.repository import Soup
  2. from gi.repository import GLib
  3.  
  4. TEST_URL = 'wss://echo.websocket.org'
  5.  
  6. def connect():
  7.     session = Soup.Session()
  8.     message = Soup.Message.new('GET', TEST_URL)
  9.     session.websocket_connect_async(message,
  10.                                     None,
  11.                                     None,
  12.                                     None,
  13.                                     _on_connect,
  14.                                     None)
  15.  
  16. def _on_connect(session, result, user_data):
  17.     con = session.websocket_connect_finish(result)
  18.     con.connect('message', _on_message)
  19.     con.connect('closed', _on_closed)
  20.     con.connect('closing', _on_closing)
  21.     con.connect('error', _on_error)
  22.     con.connect('pong', _on_pong)
  23.     con.send_message(Soup.WebsocketDataType.TEXT,
  24.                      GLib.Bytes.new('test'.encode()))
  25.  
  26. def _on_message(websocket_connection, type, message):
  27.     print('MESSAGE')
  28.     bytes_ = message.get_data()
  29.     string = bytes_.decode()
  30.     print(string)
  31.  
  32. def _on_error(websocket_connection, error):
  33.     print('ERROR')
  34.     print(error)
  35.  
  36. def _on_closed(websocket_connection):
  37.     print('CLOSED')
  38.  
  39. def _on_closing(websocket_connection):
  40.     print('CLOSING')
  41.  
  42. def _on_pong(websocket_connection, message):
  43.     print('Pong')
  44.     bytes_ = message.get_data()
  45.     string = bytes_.decode()
  46.     print(string)
  47.  
  48. connect()
  49. GLib.MainLoop().run()
  50.  

Reply to "Untitled"

Here you can reply to the paste above