How do I display UTF-8 characters sent through a websocket?

Clay

I'm trying to build a simple web socket server that loads a file with some tweets in it (as CSV) and then just sends the string of the tweet to a web browser through a websocket. Here is a gist with the sample that I'm using for testing. Here's the Autobahn server component (server.py):

import random
import time
from twisted.internet   import reactor
from autobahn.websocket import WebSocketServerFactory, \
                               WebSocketServerProtocol, \
                               listenWS


f = open("C:/mypath/parsed_tweets_sample.csv")

class TweetStreamProtocol(WebSocketServerProtocol):

    def sendTweet(self):
        tweet = f.readline().split(",")[2]
        self.sendMessage(tweet, binary=False)

    def onMessage(self, msg, binary):
        self.sendTweet() 

if __name__ == '__main__':

   factory = WebSocketServerFactory("ws://localhost:9000", debug = False)
   factory.protocol = TweetStreamProtocol
   listenWS(factory)
   reactor.run()

And here is the web component (index.html):

<html>
   <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <script type="text/javascript"> 
            var ws = new WebSocket("ws://localhost:9000");

            ws.onmessage = function(e) {
               document.getElementById('msg').textContent = e.data; //unescape(encodeURIComponent(e.data));
               console.log("Got echo: " + e.data);
            }
      </script>
   </head>
   <body>
      <h3>Twitter Stream Visualization</h3>
      <div id="msg"></div>
      <button onclick='ws.send("tweetme");'>
         Get Tweet
      </button>
   </body>
</html>

When the tweet arrives in the browser, the UTF-8 characters aren't properly displayed. How can I modify these simple scripts to display the proper UTF-8 characters in the browser?

oberstet

This works for me:

from autobahn.twisted.websocket import WebSocketServerProtocol, \
                                       WebSocketServerFactory


class TweetStreamProtocol(WebSocketServerProtocol):

   def sendTweets(self):
      for line in open('gistfile1.txt').readlines():
         ## decode UTF8 encoded file
         data = line.decode('utf8').split(',')

         ## now operate on data using Python string functions ..

         ## encode and send payload
         payload = data[2].encode('utf8')
         self.sendMessage(payload)

      self.sendMessage((u"\u03C0"*10).encode("utf8"))

   def onMessage(self, payload, isBinary):
      if payload == "tweetme":
         self.sendTweets()



if __name__ == '__main__':

   import sys

   from twisted.python import log
   from twisted.internet import reactor

   log.startLogging(sys.stdout)

   factory = WebSocketServerFactory("ws://localhost:9000", debug = False)
   factory.protocol = TweetStreamProtocol

   reactor.listenTCP(9000, factory)
   reactor.run()

Notes:

  • above code is for Autobahn|Python 0.7 and above
  • I'm not sure if you sample Gist is properly UTF8 encoded file
  • However, the "last" pseudo Tweet is 10x "pi", and that properly shows in the browser, so it works in principle ..

Also note: for reasons too long to explain here, Autobahn's sendMessage function expects payload to be already UTF8 encoded if isBinary == False. A "normal" Python string is Unicode, which needs to be encoded like above to UTF8 for sending.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I convert UTF-8 special characters in Bash?

From Dev

how do you make emacs-w32 display utf-8 characters?

From Dev

Messages from .properties file do not display UTF-8 characters

From Dev

How to change bash console font to display UTF-8 characters

From Dev

How to display UTF-8 characters in RAILS CONSOLE?

From Dev

In Ruby, how do I deal with non-UTF 8 characters in PDF content?

From Dev

how do I sent escape character ^] through a spawned telnet session?

From Dev

using CListCtrl to display utf-8 characters

From Dev

using CListCtrl to display utf-8 characters

From Dev

How do I write utf-8 characters( '\xe7\x8e\xa9' ) into another file as Chinese characters?

From Dev

How can I display utf8 in powershell?

From Dev

How do I make the toString() of the JSONObject encode the UTF-8 characters to unicode like in json_encode of PHP?

From Dev

REST Yii2 - how to display non-UTF8 characters coming from database in json?

From Dev

How do I cycle through a list of characters and replace them with "_" in Python?

From Dev

How do I iterate through an array and change characters efficiently?

From Dev

How do I commit with a utf-8 message file?

From Dev

How do I properly handle &#xFFFF; in UTF-8 XML?

From Dev

How do I unescape multiple byte character utf8

From Dev

How do I get MinTTY working with UTF8

From Dev

How do I display all the characters between two specific strings?

From Dev

Python: How to print with UTF-8 characters?

From Dev

How do I iterate through an object but display then in separate rows?

From Dev

Display chinese characters WITHOUT using utf8 encoding?

From Dev

Unable to display Japanese (UTF-8) characters in email body with webbrowser

From Dev

How do I check if a message sent through socket-io is read?

From Dev

How do you make a better seo friendly URL when there is UTF-8 characters and many space in between?

From Dev

How can I make System.in Input Stream read utf-8 characters?

From Dev

How can I use Net::Http to download a file with UTF-8 characters in it?

From Dev

How I can use Java Regex for Turkish characters to UTF-8

Related Related

  1. 1

    How do I convert UTF-8 special characters in Bash?

  2. 2

    how do you make emacs-w32 display utf-8 characters?

  3. 3

    Messages from .properties file do not display UTF-8 characters

  4. 4

    How to change bash console font to display UTF-8 characters

  5. 5

    How to display UTF-8 characters in RAILS CONSOLE?

  6. 6

    In Ruby, how do I deal with non-UTF 8 characters in PDF content?

  7. 7

    how do I sent escape character ^] through a spawned telnet session?

  8. 8

    using CListCtrl to display utf-8 characters

  9. 9

    using CListCtrl to display utf-8 characters

  10. 10

    How do I write utf-8 characters( '\xe7\x8e\xa9' ) into another file as Chinese characters?

  11. 11

    How can I display utf8 in powershell?

  12. 12

    How do I make the toString() of the JSONObject encode the UTF-8 characters to unicode like in json_encode of PHP?

  13. 13

    REST Yii2 - how to display non-UTF8 characters coming from database in json?

  14. 14

    How do I cycle through a list of characters and replace them with "_" in Python?

  15. 15

    How do I iterate through an array and change characters efficiently?

  16. 16

    How do I commit with a utf-8 message file?

  17. 17

    How do I properly handle &#xFFFF; in UTF-8 XML?

  18. 18

    How do I unescape multiple byte character utf8

  19. 19

    How do I get MinTTY working with UTF8

  20. 20

    How do I display all the characters between two specific strings?

  21. 21

    Python: How to print with UTF-8 characters?

  22. 22

    How do I iterate through an object but display then in separate rows?

  23. 23

    Display chinese characters WITHOUT using utf8 encoding?

  24. 24

    Unable to display Japanese (UTF-8) characters in email body with webbrowser

  25. 25

    How do I check if a message sent through socket-io is read?

  26. 26

    How do you make a better seo friendly URL when there is UTF-8 characters and many space in between?

  27. 27

    How can I make System.in Input Stream read utf-8 characters?

  28. 28

    How can I use Net::Http to download a file with UTF-8 characters in it?

  29. 29

    How I can use Java Regex for Turkish characters to UTF-8

HotTag

Archive