Skip to content

Commit

Permalink
small changes and documentation updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jebej committed Sep 18, 2018
1 parent e995b23 commit f52f65f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 42 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@ MatlabWebSocket is a simple library consisting of a websocket server and client

Installation and Uninstallation
------------
First, download the latest release on GitHub or MATLAB Central and extract to contents where you want.

The required java library is a `jar` file located in the `/jar/` folder. It must be placed on the static java class path in MATLAB. For example, if the location of the jar file is `C:\MatlabWebSocket\jar\matlab-websocket-1.3.jar`, then open the static class path file with the following command:
*IMPORTANT*: you must make sure to install the java library to the static class path by following the instructions below. MatlabWebSocket will not work otherwise!

First, download the latest release on GitHub or MATLAB Central and extract the contents where you want.

The required java library is a `jar` file located in the `/jar/` folder. It must be placed on the static java class path in MATLAB. For example, if the location of the jar file is `C:\MatlabWebSocket\jar\matlab-websocket-*.*.jar`, then open the static class path file with the following command:
```matlab
edit(fullfile(prefdir,'javaclasspath.txt'))
```
and add the line `C:\MatlabWebSocket\jar\matlab-websocket-1.3.jar` to it. Make sure that there are no other lines with a `matlab-websocket-*` jar.
and add the line `C:\MatlabWebSocket\jar\matlab-websocket-*.*.jar` to it. Make sure that there are no other lines with a `matlab-websocket-*` entry.

Make sure to replace the stars `matlab-websocket-*.*.jar` with the correct version number that you downloaded.

See the [MATLAB Documentation](http://www.mathworks.com/help/matlab/matlab_external/static-path.html) for more information on the static java class path. Note that after adding the jar file to the java class path, MATLAB will need to be restarted.
After having done this, restart MATLAB and check that the line was read by MATLAB properly by running the `javaclasspath` command. The line should appear at the bottom of the list, before the `DYNAMIC JAVA PATH` entries. Note that seeing the entry here does not mean that MATLAB necessarily found the jar file properly. You must make sure that the actual `jar` file is indeed at this location.

You must now add the `/src/` folder to the MATLAB path. If you want to run the examples, add the `/examples/` folder as well.

Simply undo these operations to uninstall MatlabWebSocket.

See the [MATLAB Documentation](http://www.mathworks.com/help/matlab/matlab_external/static-path.html) for more information on the static java class path.

Usage
------------

The `WebSocketServer.m` file is an abstract MATLAB class. The behaviour of the server must therefore be defined by creating a subclass that implements the following methods:
To implement a WebSocket server or client, a subclass of either `WebSocketServer` or `WebSocketClient` must be defined. For more details (see the [object-oriented programming documentation of MATLAB](http://www.mathworks.com/help/matlab/object-oriented-programming.html)).

The `WebSocketServer.m` file is an abstract MATLAB class. The behavior of the server must therefore be defined by creating a subclass that implements the following methods:

```matlab
onOpen(obj,conn,message)
Expand Down Expand Up @@ -50,7 +59,7 @@ Example
------------
The example is an echo server, it returns to the client whatever was received.

Run the echo server by making sure that the file is on the MATLAB path and executing
Run the echo server by making sure that the example file `EchoServer.m` is on the MATLAB path and executing
```matlab
server = EchoServer(30000)
```
Expand Down
26 changes: 17 additions & 9 deletions src/WebSocketClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,24 @@

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
% WebSocketClient Constructor
% Creates a java client to connect to the designated server.
% The URI must be of the form 'ws://some.server.org:30000'.
obj.URI = lower(URI);
if strfind(obj.URI,'wss')
obj.Secure = true;
end
if obj.Secure && nargin == 4
obj.UseKeyStore = true;
obj.KeyStore = keyStore;
obj.StorePassword = storePassword;
obj.KeyPassword = keyPassword;
elseif obj.Secure && nargin ~= 1
error('Invalid number of arguments for secure connection with keystore!');
elseif ~obj.Secure && nargin > 1
warning(['You are passing a keystore, but the given '...
'server URI does not start with "wss". '...
'The connection will not be secure.']);
end
% Connect the client to the server
obj.open();
Expand All @@ -58,15 +67,14 @@ 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
import io.github.jebej.matlabwebsocket.*
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');
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
Expand Down
2 changes: 1 addition & 1 deletion src/WebSocketConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function delete(obj)
function send(obj,message)
% Send a message to that WebSocket client.
if ~obj.Status; error('Connection is closed!'); end
if ~isa(message,'char') && ~isa(message,'int8');
if ~isa(message,'char') && ~isa(message,'int8')
error('You can only send character arrays or int8 arrays!');
end
obj.WebSocketObj.send(message);
Expand Down
42 changes: 22 additions & 20 deletions src/WebSocketServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,27 @@
function obj = WebSocketServer(port,keyStore,storePassword,keyPassword)
% Constructor
obj.Port = port;
if nargin>1
if nargin == 4
obj.Secure = true;
obj.KeyStore = keyStore;
obj.StorePassword = storePassword;
obj.KeyPassword = keyPassword;
elseif nargin ~= 1
error('Invalid number of arguments for secure connection with keystore!');
end
% Start server
obj.start();
end

function conns = get.Connections(obj)
% Get current connections as a struct, listing HashCode,
% Address and Port, use the struct2table method on the returned
% struct for a better display
connArr = obj.ServerObj.connections.toArray;
N = size(connArr,1);
conns = cell(N,3);
for n = 1:N
conns{n,1} = int32(connArr(n).hashCode());
conns{n,2} = char(connArr(n).getRemoteSocketAddress.getHostName());
conns{n,3} = int32(connArr(n).getRemoteSocketAddress.getPort());
end
conns = cell2struct(conns,{'HashCode','Address','Port'},2);
end

function start(obj)
% Start the WebSocket server
if obj.Status; error('The server is already running'); end
import io.github.jebej.matlabwebsocket.*
% Create the java server object in with specified port
if obj.Secure
import io.github.jebej.matlabwebsocket.MatlabWebSocketSSLServer;
obj.ServerObj = handle(MatlabWebSocketSSLServer(obj.Port,obj.KeyStore,obj.StorePassword,obj.KeyPassword),'CallbackProperties');
obj.ServerObj = handle(MatlabWebSocketSSLServer(obj.Port,...
obj.KeyStore,obj.StorePassword,obj.KeyPassword),'CallbackProperties');
else
import io.github.jebej.matlabwebsocket.MatlabWebSocketServer;
obj.ServerObj = handle(MatlabWebSocketServer(obj.Port),'CallbackProperties');
end
% Set callbacks
Expand All @@ -83,7 +70,7 @@ function start(obj)
function stop(obj,timeout)
% Stop the server with a timeout to close connections
if ~obj.Status; error('The server is not running!'); end
if nargin<2; timeout=5000; end;
if nargin<2; timeout=5000; end
obj.ServerObj.stop(int32(timeout));
% Explicitely delete the server object
delete(obj.ServerObj); obj.ServerObj=[];
Expand All @@ -98,6 +85,21 @@ function delete(obj)
end
end

function conns = get.Connections(obj)
% Get current connections as a struct, listing HashCode,
% Address and Port, use the struct2table method on the returned
% struct for a better display
connArr = obj.ServerObj.connections.toArray;
N = size(connArr,1);
conns = cell(N,3);
for n = 1:N
conns{n,1} = int32(connArr(n).hashCode());
conns{n,2} = char(connArr(n).getRemoteSocketAddress.getHostName());
conns{n,3} = int32(connArr(n).getRemoteSocketAddress.getPort());
end
conns = cell2struct(conns,{'HashCode','Address','Port'},2);
end

function conn = getConnection(obj,hashCode)
% Get a WebSocketConnection to the client identified by the
% HashCode
Expand Down
11 changes: 5 additions & 6 deletions test/runtests.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
function runtests( TEST_SSL )
%RUNTESTS Summary of this function goes here
% Detailed explanation goes here
%RUNTESTS These tests need the example folder to be on the path to work
if nargin==0; TEST_SSL = 0; end

PROTOCOL = 'ws';
Expand Down Expand Up @@ -45,21 +44,21 @@ function runtests( TEST_SSL )

% Large file transfer test
A = randi([-128 127],3*10^7,1,'int8');
c.send(A); pause(0.3);
c.send(A); pause(1);

% Connect many clients
N = 100;
clients = cell(N,1);
for i=1:N; clients{i} = SimpleClient(URI,STORE,STOREPASSWORD,KEYPASSWORD); end
pause(0.1);
pause(1);

% Send a message from each client
for i=1:N; clients{i}.send(A(i*(1:10^5))); end
pause(0.5);
pause(1);

% Disconnect all the clients
for i=1:N; delete(clients{i}); end
pause(0.1);
pause(1);

catch err
delete(c);
Expand Down

0 comments on commit f52f65f

Please sign in to comment.