日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

matlab用socket线程发送数据,使用Python Twisted和Autobahn从Matlab通过WebSocket发送JSON数据...

發(fā)布時(shí)間:2023/12/31 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 matlab用socket线程发送数据,使用Python Twisted和Autobahn从Matlab通过WebSocket发送JSON数据... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我正在嘗試創(chuàng)建一個(gè)從Matlab到WebSocket流JSON幀的連接。我用下面的代碼測(cè)試了我的python安裝和twisted。在

工作實(shí)例

Matlab代碼

示例驅(qū)動(dòng)程序代碼,它使用JSONlab工具箱將Matlab數(shù)據(jù)轉(zhuǎn)換為JSON格式,然后對(duì)數(shù)據(jù)進(jìn)行Icompress和Base64編碼。因?yàn)槲疫€沒有讓RPC工作,所以我使用命令行,我需要壓縮和Base64編碼來避免行長(zhǎng)度和shell轉(zhuǎn)義問題。在clear all

close all

python = '/usr/local/bin/python'

bc = '/Users/palmerc/broadcast_client.py'

i = uint32(1)

encoder = org.apache.commons.codec.binary.Base64

while true

tic;

packet = rand(100, 100);

json_packet = uint8(savejson('', packet));

compressed = CompressLib.compress(json_packet);

b64 = char(encoder.encode(compressed));

message = sprintf('%s %s %s', python, bc, b64);

status = system(message);

i = i + 1;

toc;

end

廣播客戶端代碼

客戶端代碼有兩種調(diào)用方式。您可以通過命令行傳遞消息,也可以創(chuàng)建BroadcastClient實(shí)例并調(diào)用sendMessage。在

^{pr2}$

廣播服務(wù)器代碼

服務(wù)器使用TXJSONRPC、Twisted和Autobahn在7080上提供RPC客戶端,在8080上提供web客戶端,在9080上提供WebSocket。Autobahn Web Client對(duì)調(diào)試很有用,應(yīng)該與服務(wù)器代碼放在同一個(gè)目錄中。在#!/usr/bin/env python

import sys

from twisted.internet import reactor

from twisted.python import log

from twisted.web.server import Site

from twisted.web.static import File

from txjsonrpc.web import jsonrpc

from autobahn.twisted.websocket import WebSocketServerFactory, \

WebSocketServerProtocol, \

listenWS

class BroadcastServerProtocol(WebSocketServerProtocol):

def onOpen(self):

self.factory.registerClient(self)

def onMessage(self, payload, isBinary):

if not isBinary:

message = "{} from {}".format(payload.decode('utf8'), self.peer)

self.factory.broadcastMessage(message)

def connectionLost(self, reason):

WebSocketServerProtocol.connectionLost(self, reason)

self.factory.unregisterClient(self)

class BroadcastServerFactory(WebSocketServerFactory):

"""

Simple broadcast server broadcasting any message it receives to all

currently connected clients.

"""

def __init__(self, url, debug=False, debugCodePaths=False):

WebSocketServerFactory.__init__(self, url, debug=debug, debugCodePaths=debugCodePaths)

self.clients = []

def registerClient(self, client):

if client not in self.clients:

print("registered client {}".format(client.peer))

self.clients.append(client)

def unregisterClient(self, client):

if client in self.clients:

print("unregistered client {}".format(client.peer))

self.clients.remove(client)

def broadcastMessage(self, message):

print("broadcasting message '{}' ..".format(message))

for client in self.clients:

client.sendMessage(message.encode('utf8'))

print("message sent to {}".format(client.peer))

class BroadcastPreparedServerFactory(BroadcastServerFactory):

"""

Functionally same as above, but optimized broadcast using

prepareMessage and sendPreparedMessage.

"""

def broadcastMessage(self, message):

print("broadcasting prepared message '{}' ..".format(message))

preparedMessage = self.prepareMessage(message.encode('utf8'), isBinary=False)

for client in self.clients:

client.sendPreparedMessage(preparedMessage)

print("prepared message sent to {}".format(client.peer))

class MatlabClient(jsonrpc.JSONRPC):

factory = None

def jsonrpc_broadcastMessage(self, message):

if self.factory is not None:

print self.factory.broadcastMessage(message)

if __name__ == '__main__':

if len(sys.argv) > 1 and sys.argv[1] == 'debug':

log.startLogging(sys.stdout)

debug = True

else:

debug = False

factory = BroadcastPreparedServerFactory(u"ws://127.0.0.1:9000",

debug=debug,

debugCodePaths=debug)

factory.protocol = BroadcastServerProtocol

listenWS(factory)

matlab = MatlabClient()

matlab.factory = factory

reactor.listenTCP(7080, Site(matlab))

webdir = File(".")

web = Site(webdir)

reactor.listenTCP(8080, web)

reactor.run()

問題-失敗的嘗試

首先要注意的是,如果在Matlab中使用python有困難,那么需要確保使用pyversion命令在系統(tǒng)上指向正確的python版本,并且可以使用pyversion('/path/to/python')來更正它

Matlab無法運(yùn)行reactorclear all

close all

i = uint32(1)

while true

tic;

packet = rand(100, 100);

json_packet = uint8(savejson('', packet));

compressed = CompressLib.compress(json_packet);

b64 = char(encoder.encode(compressed));

bc.sendMessage(py.str(b64.'));

py.twisted.internet.reactor.run % This won't work.

i = i + 1;

toc;

end

Matlab POST

另一個(gè)嘗試涉及使用Matlab的webwrite來發(fā)布到服務(wù)器。結(jié)果表明,webwrite只需傳遞正確的weboptions,就可以將數(shù)據(jù)轉(zhuǎn)換為JSON。在options = weboptions('MediaType', 'application/json');

data = struct('Matrix', rand(100, 100));

webwrite(server, data, options);

這是有效的,但結(jié)果證明每個(gè)消息都很慢(約0.1秒)。我應(yīng)該提到的是,矩陣并不是我發(fā)送的真實(shí)數(shù)據(jù),真實(shí)數(shù)據(jù)序列化為每條消息280000字節(jié),但這提供了一個(gè)合理的近似值。在

我怎樣才能調(diào)用bc.sendMessage以便它能夠正確地讓reactor運(yùn)行,或者以另一種更快的方式解決這個(gè)問題?在

總結(jié)

以上是生活随笔為你收集整理的matlab用socket线程发送数据,使用Python Twisted和Autobahn从Matlab通过WebSocket发送JSON数据...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。