Untitled

From lovetox, 1 Month ago, written in Python, viewed 22 times. This paste will run down the curtain in 1 Second.
URL https://paste.gajim.org/view/bba8bcbf Embed
Download Paste or View Raw
  1. from gi.repository import Soup
  2. from gi.repository import GLib
  3.  
  4. import time
  5.  
  6. TEST_URL = 'wss://echo.websocket.org'
  7.  
  8. print(Soup.get_major_version(),
  9.       Soup.get_minor_version(),
  10.       Soup.get_micro_version())
  11.  
  12.  
  13. con = None
  14.  
  15.  
  16. def connect():
  17.     session = Soup.Session()
  18.     session.add_feature(Soup.Logger.new(Soup.LoggerLogLevel.BODY, -1))
  19.     message = Soup.Message.new('GET', TEST_URL)
  20.     message.connect('network-event', print)
  21.     message.connect('starting', print)
  22.     session.websocket_connect_async(message,
  23.                                     None,
  24.                                     None,
  25.                                     None,
  26.                                     _on_connect,
  27.                                     None)
  28.  
  29. def _on_connect(session, result, user_data):
  30.     global con
  31.     con = session.websocket_connect_finish(result)
  32.     con.set_keepalive_interval(5)
  33.     con.connect('message', _on_message)
  34.     con.connect('closed', _on_closed)
  35.     con.connect('closing', _on_closing)
  36.     con.connect('error', _on_error)
  37.     con.connect('pong', _on_pong)
  38.     print('Trying to send message')
  39.     con.send_text('test')
  40.  
  41. def _on_message(websocket_connection, type, message):
  42.     print('MESSAGE')
  43.     bytes_ = message.get_data()
  44.     string = bytes_.decode()
  45.     print(string)
  46.  
  47. def _on_error(websocket_connection, error):
  48.     print('ERROR')
  49.     print(error)
  50.  
  51. def _on_closed(websocket_connection):
  52.     print('CLOSED')
  53.  
  54. def _on_closing(websocket_connection):
  55.     print('CLOSING')
  56.  
  57. def _on_pong(websocket_connection, message):
  58.     print('Pong')
  59.     bytes_ = message.get_data()
  60.     string = bytes_.decode()
  61.     print(string)
  62.  
  63. connect()
  64. GLib.MainLoop().run()
  65.  

Reply to "Untitled"

Here you can reply to the paste above