繁体   English   中英

如何显示通过websocket发送的UTF-8字符?

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

我正在尝试构建一个简单的Web套接字服务器,它在其中加载一些包含一些推文的文件(作为CSV),然后通过websocket将该推文的字符串发送到Web浏览器。 这是我用于测试的样本的要点。 这是Autobahn服务器组件( 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()

这是web组件( 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>

当推文到达浏览器时,UTF-8字符未正确显示。 如何修改这些简单脚本以在浏览器中显示正确的UTF-8字符?

这对我有用:

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()

笔记:

  • 上面的代码适用于Autobahn | Python 0.7及以上版本
  • 我不确定你是否对Gist进行了正确的UTF8编码文件
  • 但是,“最后”伪推文是10x“pi”,并且在浏览器中正确显示,因此它原则上有效。

另请注意:由于原因太长而无法在此解释,如果isBinary == False ,Autobahn的sendMessage函数要求payload已经是UTF8编码。 “普通”Python字符串是Unicode,需要像上面那样编码为UTF8才能发送。

而不是< meta http-equiv="content-type" content="text/html; charset=UTF-8"> < try < meta charset = utf-8>
如果你正在使用XHTML,那么写<meta charset = utf-8 />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM