How to use the ws4py.messaging.TextMessage function in ws4py

To help you get started, we’ve selected a few ws4py examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Lawouach / WebSocket-for-Python / test / test_stream.py View on Github external
def test_helper_with_bytes_text_message(self):
        s = Stream()
        m = s.text_message('hello there!')
        self.assertIsInstance(m, TextMessage)
        self.assertFalse(m.is_binary)
        self.assertTrue(m.is_text)
        self.assertEqual(m.opcode, OPCODE_TEXT)
        self.assertEqual(m.encoding, 'utf-8')
        self.assertIsInstance(m.data, bytes)
        self.assertEqual(len(m), 12)
        self.assertEqual(len(m.data), 12)
        self.assertEqual(m.data, b'hello there!')
github Lawouach / WebSocket-for-Python / test / test_websocket.py View on Github external
def test_send_bytes_with_masking(self):
        tm = TextMessage(b'hello world').single(mask=True)
        
        m = MagicMock()
        ws = WebSocket(sock=m)
        ws.stream = MagicMock()
        ws.stream.always_mask = True
        ws.stream.text_message.return_value.single.return_value = tm
        
        ws.send(b'hello world')
        m.sendall.assert_called_once_with(tm)
github domogik / domoweb / domoweb / mqModel.py View on Github external
def on_message(self, msgid, content):
        cherrypy.log("QM New pub message : [%s]" % msgid)
        msg = json.dumps({'id':msgid, 'content':content})
        cherrypy.engine.publish('websocket-broadcast', TextMessage(msg))
        self.callback(content)
github claritylab / lucida / lucida / speechrecognition / kaldi_gstreamer_asr / kaldigstserver / worker.py View on Github external
thread.start_new_thread(self.guard_timeout, ())
			logger.info("%s: Started timeout guard" % self.request_id)
			logger.info("%s: Initialized request" % self.request_id)
			self.state = self.STATE_INITIALIZED
		elif m.data == "EOS":
			if self.state != self.STATE_CANCELLING and self.state != self.STATE_EOS_RECEIVED and self.state != self.STATE_FINISHED:
				self.decoder_pipeline.end_request()
				self.state = self.STATE_EOS_RECEIVED
			else:
				logger.info("%s: Ignoring EOS, worker already in state %d" % (self.request_id, self.state))
		else:
			if self.state != self.STATE_CANCELLING and self.state != self.STATE_EOS_RECEIVED and self.state != self.STATE_FINISHED:
				if isinstance(m, ws4py.messaging.BinaryMessage):
					self.decoder_pipeline.process_data(m.data)
					self.state = self.STATE_PROCESSING
				elif isinstance(m, ws4py.messaging.TextMessage):
					props = json.loads(str(m))
					if 'adaptation_state' in props:
						as_props = props['adaptation_state']
						if as_props.get('type', "") == "string+gzip+base64":
							adaptation_state = zlib.decompress(base64.b64decode(as_props.get('value', '')))
							logger.info("%s: Setting adaptation state to user-provided value" % (self.request_id))
							self.decoder_pipeline.set_adaptation_state(adaptation_state)
						else:
							logger.warning("%s: Cannot handle adaptation state type " % (self.request_id, as_props.get('type', "")))
					else:
						logger.warning("%s: Got JSON message but don't know what to do with it" % (self.request_id))
			else:
				logger.info("%s: Ignoring data, worker already in state %d" % (self.request_id, self.state))
github Lawouach / WebSocket-for-Python / ws4py / streaming.py View on Github external
def text_message(self, text):
        """
        Returns a :class:`ws4py.messaging.TextMessage` instance
        ready to be built. Convenience method so
        that the caller doesn't need to import the
        :class:`ws4py.messaging.TextMessage` class itself.
        """
        return TextMessage(text=text)
github edvm / pysenteishon / pysenteishon / app.py View on Github external
def closed(self, code, reason="A user left pysenteishon, seems the talk is too boring :("):
        cherrypy.engine.publish('/slides', TextMessage(reason))
github Lawouach / WebSocket-for-Python / example / echo_cherrypy_server.py View on Github external
def closed(self, code, reason="A client left the room without a proper explanation."):
        cherrypy.engine.publish('websocket-broadcast', TextMessage(reason))
github domogik / domoweb / corePlugin.py View on Github external
def loader(self, status):
        """
        CherryPy WSGI server doesn't offer a log
        facility, we add a straightforward WSGI middleware to do so, based
        on the CherryPy built-in logger.
        """
        if status == 'finished':
            self.bus.log("Mounting the Django application")
            cherrypy.tree.graft(HTTPLogger(WSGIHandler()))
            cherrypy.engine.publish('websocket-broadcast', TextMessage('domoweb-ready'))
github Lawouach / WebSocket-for-Python / ws4py / streaming.py View on Github external
# it anyway. Therefore, on py2k, the bytes
                            # are actually a str object and can't be used
                            # in the utf8 validator as we need integers
                            # when we get each byte one by one.
                            # Our only solution here is to convert our
                            # string to a bytearray.
                            some_bytes = bytearray(some_bytes)

                    if frame.opcode == OPCODE_TEXT:
                        if self.message and not self.message.completed:
                            # We got a text frame before we completed the previous one
                            msg = CloseControlMessage(code=1002, reason='Received a new message before completing previous')
                            self.errors.append(msg)
                            break

                        m = TextMessage(some_bytes)
                        m.completed = (frame.fin == 1)
                        self.message = m

                        if some_bytes:
                            is_valid, end_on_code_point, _, _ = utf8validator.validate(some_bytes)

                            if not is_valid or (m.completed and not end_on_code_point):
                                self.errors.append(CloseControlMessage(code=1007, reason='Invalid UTF-8 bytes'))
                                break

                    elif frame.opcode == OPCODE_BINARY:
                        if self.message and not self.message.completed:
                            # We got a text frame before we completed the previous one
                            msg = CloseControlMessage(code=1002, reason='Received a new message before completing previous')
                            self.errors.append(msg)
                            break
github alumae / kaldi-gstreamer-server / kaldigstserver / worker.py View on Github external
thread.start_new_thread(self.guard_timeout, ())
            logger.info("%s: Started timeout guard" % self.request_id)
            logger.info("%s: Initialized request" % self.request_id)
            self.state = self.STATE_INITIALIZED
        elif m.data == "EOS":
            if self.state != self.STATE_CANCELLING and self.state != self.STATE_EOS_RECEIVED and self.state != self.STATE_FINISHED:
                self.decoder_pipeline.end_request()
                self.state = self.STATE_EOS_RECEIVED
            else:
                logger.info("%s: Ignoring EOS, worker already in state %d" % (self.request_id, self.state))
        else:
            if self.state != self.STATE_CANCELLING and self.state != self.STATE_EOS_RECEIVED and self.state != self.STATE_FINISHED:
                if isinstance(m, ws4py.messaging.BinaryMessage):
                    self.decoder_pipeline.process_data(m.data)
                    self.state = self.STATE_PROCESSING
                elif isinstance(m, ws4py.messaging.TextMessage):
                    props = json.loads(str(m))
                    if 'adaptation_state' in props:
                        as_props = props['adaptation_state']
                        if as_props.get('type', "") == "string+gzip+base64":
                            adaptation_state = zlib.decompress(base64.b64decode(as_props.get('value', '')))
                            logger.info("%s: Setting adaptation state to user-provided value" % (self.request_id))
                            self.decoder_pipeline.set_adaptation_state(adaptation_state)
                        else:
                            logger.warning("%s: Cannot handle adaptation state type " % (self.request_id, as_props.get('type', "")))
                    else:
                        logger.warning("%s: Got JSON message but don't know what to do with it" % (self.request_id))
            else:
                logger.info("%s: Ignoring data, worker already in state %d" % (self.request_id, self.state))