-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebSocketClient.m
executable file
·149 lines (135 loc) · 5.76 KB
/
WebSocketClient.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
classdef WebSocketClient < handle
%WEBSOCKETCLIENT WebSocketClient is an ABSTRACT class that allows
%MATLAB to start a java-websocket client instance and connect to a
%WebSocket server.
%
% In order to make a valid implementation of the class, some methods
% must be defined in the superclass:
% onOpen(obj,message)
% onTextMessage(obj,message)
% onBinaryMessage(obj,bytearray)
% onError(obj,message)
% onClose((obj,message)
% The "callback" behavior of the client can be defined there. If
% the client needs to perform actions that are not responses to a
% server-caused event, these actions must be performed outside of the
% callback methods.
properties (SetAccess = private)
URI % The URI of the server
Secure = false % True if the connection is using WebSocketSecure
Status = false % Status of the connection, true if the connection is open
ClientObj % Java-WebSocket client object
end
properties (Access = private)
UseKeyStore = false
KeyStore % Location of the keystore
StorePassword % Keystore password
KeyPassword % Key password
end
methods
function obj = WebSocketClient(URI,keyStore,storePassword,keyPassword)
% Constructor, create a client to connect to the deisgnated
% server, the URI must be of the form 'ws://localhost:30000'
obj.URI = URI;
if any(regexpi(URI,'wss')); obj.Secure = true; end
if nargin>1
obj.UseKeyStore = true;
obj.KeyStore = keyStore;
obj.StorePassword = storePassword;
obj.KeyPassword = keyPassword;
end
% Connect the client to the server
obj.open();
end
function status = get.Status(obj)
% Get the status of the connection
if isempty(obj.ClientObj)
status = false;
else
status = obj.ClientObj.isOpen();
end
end
function open(obj)
% Open the connection to the server
% Create the java client object in with specified URI
if obj.Status; warning('Connection is already open!');return; end
uri = handle(java.net.URI(obj.URI));
if obj.Secure && ~obj.UseKeyStore
import io.github.jebej.matlabwebsocket.MatlabWebSocketSSLClient;
obj.ClientObj = handle(MatlabWebSocketSSLClient(uri),'CallbackProperties');
elseif obj.Secure && obj.UseKeyStore
import io.github.jebej.matlabwebsocket.MatlabWebSocketSSLClient;
obj.ClientObj = handle(MatlabWebSocketSSLClient(uri,obj.KeyStore,obj.StorePassword,obj.KeyPassword),'CallbackProperties');
else
import io.github.jebej.matlabwebsocket.MatlabWebSocketClient;
obj.ClientObj = handle(MatlabWebSocketClient(uri),'CallbackProperties');
end
% Set callbacks
set(obj.ClientObj,'OpenCallback',@obj.openCallback);
set(obj.ClientObj,'TextMessageCallback',@obj.textMessageCallback);
set(obj.ClientObj,'BinaryMessageCallback',@obj.binaryMessageCallback);
set(obj.ClientObj,'ErrorCallback',@obj.errorCallback);
set(obj.ClientObj,'CloseCallback',@obj.closeCallback);
% Connect to the websocket server
obj.ClientObj.connectBlocking();
end
function close(obj)
% Close the websocket connection and explicitely delete the
% java client object
if ~obj.Status; warning('Connection is already closed!');return; end
obj.ClientObj.closeBlocking()
delete(obj.ClientObj);
obj.ClientObj = [];
end
function delete(obj)
% Destructor
% Closes the websocket if it's open.
if obj.Status
obj.close();
end
end
function send(obj,message)
% Send a message to the server
if ~obj.Status; warning('Connection is closed!');return; end
if ~isa(message,'char') && ~isa(message,'int8')
error('You can only send character arrays or int8 arrays!');
end
obj.ClientObj.send(message);
end
end
% Implement these methods in a subclass.
methods (Abstract, Access = protected)
onOpen(obj,message)
onTextMessage(obj,message)
onBinaryMessage(obj,bytearray)
onError(obj,message)
onClose(obj,message)
end
% Private methods triggered by the callbacks defined above.
methods (Access = private)
function openCallback(obj,~,e)
% Define behavior in an onOpen method of a subclass
obj.onOpen(char(e.message));
end
function textMessageCallback(obj,~,e)
% Define behavior in an onTextMessage method of a subclass
obj.onTextMessage(char(e.message));
end
function binaryMessageCallback(obj,~,e)
% Define behavior in an onBinaryMessage method of a subclass
obj.onBinaryMessage(e.blob.array);
end
function errorCallback(obj,~,e)
% Define behavior in an onError method of a subclass
obj.onError(char(e.message));
end
function closeCallback(obj,~,e)
% Define behavior in an onClose method of a subclass
obj.onClose(char(e.message));
% Delete java client object if needed
if ~isvalid(obj); return; end
delete(obj.ClientObj);
obj.ClientObj = [];
end
end
end