MatlabWebSocket
MatlabWebSocketisasimplelibrarycosistigofawebsocketserveradclietforMATLABbuiltoJava-WebSocket,ajavaimplemetatioofthewebsocketprotocol.Ecryptioissupportedwithself-sigedcertificatesmadewiththejavakeytool.
IstallatioadUistallatioIMPORTANT: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.
UsageToimplemetaWebSocketserverorcliet,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.
ExampleTheexampleisaechoserver,itreturstotheclietwhateverwasreceived.
RutheechoserverbymakigsurethattheexamplefileEchoServer.misotheMATLABpathadexecutig
server=EchoServer(30000)tostarttheserveroport30000.
Totesttheserver,makeaclietobjectfromtheSimpleCliet.mclass:
>>cliet=SimpleCliet('ws://localhost:30000');Coectedtoserveratws://localhost:30000Cliet1063680447at127.0.0.1:42520opeedacoectioYoucaowcoectadsedmessages(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:30000Todeletetheserver,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!BuildigtheJavaJARTobuildthejarfileyourself,itisrecommededtouseApacheMave.MavewillautomaticallytakecareofdowloadigJava-WebSocketadeatlypackageeverythigitoasiglefile(a"uberjar").
Ocethemvcommadisoyourpath,simplycdtothematlab-websocketfolderadexecutemvpackage.
AckowledgmetsThisworkwasispiredbyawebsocketclietMATLABimplemetatio:matlab-websockets.
ItreliesotheJava-WebSocketlibrary.
LiceseThecodeithisrepositoryislicesedudertheMITlicese.SeetheLICENSEfilefordetails.
评论