我正在试验Python的Eventlet支持,使用这个简单的例子:
import eventlet
from eventlet import wsgi
from eventlet import websocket
from eventlet.support import six
# demo app
import os
import json
import time
import random
@websocket.WebSocketWSGI
def handle(ws):
""" This is the websocket handler function. Note that we
can dispatch based on path in here, too."""
if ws.path == '/echo':
while True:
ws.send("hello")
#ws.send(json.dumps({"msg":"hello"}))
time.sleep(1)
elif ws.path == '/data':
for i in six.moves.range(10000):
ws.send("0 %s %s\n" % (i, random.random()))
eventlet.sleep(0.1)
def dispatch(environ, start_response):
""" This resolves to the web page or the websocket depending on
the path."""
if environ['PATH_INFO'] == '/data':
return handle(environ, start_response)
else:
start_response('200 OK', [('content-type', 'text/html')])
return [open(os.path.join(
os.path.dirname(__file__),
'websocket.html')).read()]
if __name__ == "__main__":
# run an example app from the command line
listener = eventlet.listen(('127.0.0.1', 7000))
print("\nVisit http://localhost:7000/ in your websocket-capable browser.\n")
wsgi.server(listener, dispatch)我不打算在Javascript中包含我拥有的整个websocket处理程序,只包括ws.onmessage方法:
ws.onmessage = function (evt)
{
console.log(evt.data)
var received_msg = evt.data;
#Do stuff, i.e var obj = JSON.parse(received_msg)
#callback(obj)
};console.log(evt.data)表示与websocket的连接是成功的(您可以假设这一切都很好)。但是,日志记录显示Blob {size: 31, type: ""}是evt.data的内容。我假设这是某种类型的响应对象,它被解释为二进制(文件)数据(尽管我可能完全错了),但我不知道该如何处理它。
我看到Blob数据通常是类文件对象的数据类型。我想我可以这样处理它,但是我真的只想来回发送json数据。我尝试过将一个数据作为JSON并发送出去,但是它也是这样做的。甚至以Blob格式接收字符串。
如何使用eventlet进行json数据传输?
发布于 2017-05-23 08:31:55
从2017到05 Eventlet websocket API不支持字符串websocket框架。补丁是受欢迎的,很简单。
你的选择:
https://stackoverflow.com/questions/44067649
复制相似问题