Esempio n. 1
0
    def onConnect(self, requestOrResponse):

        ## Negotiate either the 'binary' or the 'base64' WebSocket subprotocol
        ##
        if isinstance(requestOrResponse, protocol.ConnectionRequest):
            request = requestOrResponse
            for p in request.protocols:
                if p in self.factory._subprotocols:
                    self._binaryMode = (p != 'base64')
                    return p
            raise http.HttpException(
                http.NOT_ACCEPTABLE[0],
                "this server only speaks %s WebSocket subprotocols" %
                self.factory._subprotocols)
        elif isinstance(requestOrResponse, protocol.ConnectionResponse):
            response = requestOrResponse
            if response.protocol not in self.factory._subprotocols:
                self.failConnection(
                    protocol.WebSocketProtocol.
                    CLOSE_STATUS_CODE_PROTOCOL_ERROR,
                    "this client only speaks %s WebSocket subprotocols" %
                    self.factory._subprotocols)
            self._binaryMode = (response.protocol != 'base64')
        else:
            ## should not arrive here
            raise Exception("logic error")
Esempio n. 2
0
   def onConnect(self, request):
      """
      Callback from :func:`autobahn.websocket.interfaces.IWebSocketChannel.onConnect`
      """
      headers = {}
      for subprotocol in request.protocols:
         version, serializerId = parseSubprotocolIdentifier(subprotocol)
         if version == 2 and serializerId in self.factory._serializers.keys():
            self._serializer = self.factory._serializers[serializerId]
            return subprotocol, headers

      if self.STRICT_PROTOCOL_NEGOTIATION:
         raise http.HttpException(http.BAD_REQUEST[0], "This server only speaks WebSocket subprotocols %s" % ', '.join(self.factory.protocols))
      else:
         ## assume wamp.2.json
         self._serializer = self.factory._serializers['json']
         return None, headers