def initSocket(self):
        """ Make the connection to Python.
        """
        
        # Check WebSocket support
        if self.nodejs:
            try:
                WebSocket = window.require('ws')  # does not work on Windows?
                #WebSocket = window.require('websocket').client
            except Exception:
                # Better error message
                raise "FAIL: you need to 'npm install -g ws' (or 'websocket')."
        else:
            WebSocket = window.WebSocket
            if (WebSocket is undefined):
                window.document.body.innerHTML = 'Browser does not support WebSockets'
                raise "FAIL: need websocket"
        # Open web socket in binary mode
        self.ws = ws = WebSocket(window.flexx.ws_url)
        ws.binaryType = "arraybuffer"
        
        def on_ws_open(evt):
            window.console.info('Socket connected')
            ws.send('hiflexx ' + window.flexx_session_id)
        def on_ws_message(evt):
            window.flexx.last_msg = evt.data or evt
            msg = window.flexx.decodeUtf8(window.flexx.last_msg)
            window.flexx.command(msg)
        def on_ws_close(evt):
            self.ws = None
            msg = 'Lost connection with server'
            if evt and evt.reason:  # nodejs-ws does not have it?
                msg += ': %s (%i)' % (evt.reason, evt.code)
            if (not window.flexx.is_notebook) and (not self.nodejs):
                window.document.body.innerHTML = msg
            else:
                window.console.info(msg)
        def on_ws_error(self, evt):
            self.ws = None
            window.console.error('Socket error')
        
        # Connect
        if self.nodejs:
            ws.on('open', on_ws_open)
            ws.on('message', on_ws_message)
            ws.on('close', on_ws_close)
            ws.on('error', on_ws_error)
        else:
            ws.onopen = on_ws_open
            ws.onmessage = on_ws_message
            ws.onclose = on_ws_close
            ws.onerror = on_ws_error
dataset/ETHPy150Open zoofIO/flexx/flexx/app/clientcore.py/FlexxJS.initSocket/original