MatlabWebSocket A simple event-based WebSocket lib开源项目

我要开发同款
匿名用户2021年11月23日
89阅读

技术信息

开源地址
https://github.com/didi/di18n
授权协议
MIT License

作品详情

MatlabWebSocket

MatlabWebSocketisasimplelibrarycosistigofawebsocketserveradclietforMATLABbuiltoJava-WebSocket,ajavaimplemetatioofthewebsocketprotocol.Ecryptioissupportedwithself-sigedcertificatesmadewiththejavakeytool.

IstallatioadUistallatio

IMPORTANT:youmustmakesuretoistallthejavalibrarytothestaticclasspathbyfollowigtheistructiosbelow.MatlabWebSocketwillotworkotherwise!

First,dowloadthelatestreleaseoGitHuborMATLABCetraladextractthecotetswhereyouwat.

Therequiredjavalibraryisajarfilelocatedithe/jar/folder.ItmustbeplacedothestaticjavaclasspathiMATLAB.Forexample,ifthelocatioofthejarfileisC:\MatlabWebSocket\jar\matlab-websocket-*.*.jar,theopethestaticclasspathfilewiththefollowigcommad:

edit(fullfile(prefdir,'javaclasspath.txt'))

adaddthelieC:\MatlabWebSocket\jar\matlab-websocket-*.*.jartoit.Makesurethatthereareootherlieswithamatlab-websocket-*etry.

Makesuretoreplacethestarsmatlab-websocket-*.*.jarwiththecorrectversioumberthatyoudowloaded.

Afterhavigdoethis,restartMATLABadcheckthattheliewasreadbyMATLABproperlybyruigthejavaclasspathcommad.Thelieshouldappearatthebottomofthelist,beforetheDYNAMICJAVAPATHetries.NotethatseeigtheetryheredoesotmeathatMATLABecessarilyfoudthejarfileproperly.Youmustmakesurethattheactualjarfileisideedatthislocatio.

Youmustowaddthe/src/foldertotheMATLABpath.Ifyouwattorutheexamples,addthe/examples/folderaswell.

SimplyudotheseoperatiostouistallMatlabWebSocket.

SeetheMATLABDocumetatioformoreiformatioothestaticjavaclasspath.

Usage

ToimplemetaWebSocketserverorcliet,asubclassofeitherWebSocketServerorWebSocketClietmustbedefied.Formoredetails(seetheobject-orietedprogrammigdocumetatioofMATLAB).

TheWebSocketServer.mfileisaabstractMATLABclass.Thebehavioroftheservermustthereforebedefiedbycreatigasubclassthatimplemetsthefollowigmethods:

oOpe(obj,co,message)oTextMessage(obj,co,message)oBiaryMessage(obj,co,message)oError(obj,co,message)oClose(obj,co,message)objistheobjectistaceofthesubclass,itisimplicitlypassedbyMATLAB(seetheobject-orietedprogrammigdocumetatioofMATLAB).messageisthemessagereceivedbytheserver.Itwillusuallybeacharacterarray,exceptfortheoBiaryMessagemethod,iwhichcaseitwillbeait8arraycoisaWebSocketCoectioobjectrepresetigtheclietcoectiothatcausedtheevet.Forexample,ifamessageisreceived,thecoobjectwillrepresettheclietthatsetthismessage.Youcasedmessagestothatclietthroughthisobject.

TheWebSocketCliet.mclassisverysimilartotheserver,exceptthatocoobjectispassedtotheoSomethigmethods.

Thesemethodswillbeautomaticallycalledwhethecorrespodigevet(coectioisopeed,messagereceived,etc...)occurs.Ithisway,areactivebehaviorcabedefied.

Theserversupportsavarietyofmethodstohelptalktocliets,lookitheMATLABclassfiletoseewhatmethodsareavailable.

Wheyouaredoe,dootforgettodeletetheclietsad/orservers.

SeetheEchoServer.madSimpleCliet.mfilesitheexamplesfolderforaimplemetatioexample.AgoodresourceoclassesistheMATLABobject-orieteddocumetatio.

Example

Theexampleisaechoserver,itreturstotheclietwhateverwasreceived.

RutheechoserverbymakigsurethattheexamplefileEchoServer.misotheMATLABpathadexecutig

server=EchoServer(30000)

tostarttheserveroport30000.

Totesttheserver,makeaclietobjectfromtheSimpleCliet.mclass:

>>cliet=SimpleCliet('ws://localhost:30000');Coectedtoserveratws://localhost:30000Cliet1063680447at127.0.0.1:42520opeedacoectio

Youcaowcoectadsedmessages(cliet.sed('hi!')).Iftheserverisworkigproperly,youwillreceivemessagesideticaltotheoesyoused.

Theservercaeumerateclietsthatarecoected,justtype:

server.Coectios%viewtheresultasatable:struct2table(server.Coectios)

ThisallowsyoutosedamessagetotheclietviaitsidetifyigHashCode:

>>clietCode=server.Coectios(1).HashCode;>>server.sedTo(clietCode,'hi,thisistheserver!')Messagereceived:hi,thisistheserver!

Theservercabestoppedadrestarted(thiswilldiscoectcliets):

>>server.stopDiscoectedfromserveratws://localhost:30000

Todeletetheserver,type:

delete(server);clearserver;SSL/WebSocketSecure(wss)

ToeableSSL,youmustfirsthaveacertificate.Aself-sigedkeystorecabegeeratedwiththejavakeytool,butyoushouldalwaysuseavalidcertificateiproductio.Fromthere,opetheserverbypassigthelocatioofthestore,thestorepassword,adthekeypassword.WiththeEchoServer,forexample:

PORT=8887;%chooseaotherport!STORE='C:\keystore.jks';STOREPASSWORD='storepassword';KEYPASSWORD='keypassword';s=EchoServer(PORT,STORE,STOREPASSWORD,KEYPASSWORD);

Theclietcathecoecttoit:

URI=spritf(wss://localhost:%d',PORT);c=SimpleCliet(URI,STORE,STOREPASSWORD,KEYPASSWORD);

Ifavalidcertificateisused,thedefaultjavakeystorecabeused.Forexample,wecacoectaclietdirectlythethesecuredwebsocket.orgtestserver:

>>c2=SimpleCliet('wss://echo.websocket.org');Coectedtoserveratwss://echo.websocket.org>>c2.sed('hi,thiscommuicatioissecured!')Messagereceived:hi,thiscommuicatioissecured!BuildigtheJavaJAR

Tobuildthejarfileyourself,itisrecommededtouseApacheMave.MavewillautomaticallytakecareofdowloadigJava-WebSocketadeatlypackageeverythigitoasiglefile(a"uberjar").

Ocethemvcommadisoyourpath,simplycdtothematlab-websocketfolderadexecutemvpackage.

Ackowledgmets

ThisworkwasispiredbyawebsocketclietMATLABimplemetatio:matlab-websockets.

ItreliesotheJava-WebSocketlibrary.

Licese

ThecodeithisrepositoryislicesedudertheMITlicese.SeetheLICENSEfilefordetails.

功能介绍

MatlabWebSocket MatlabWebSocket is a simple library consisting of a websocket server and client f...

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论