Skip to content

Commit

Permalink
Updated README, small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jebej committed Sep 26, 2017
1 parent 2358a9d commit abfa30d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 21 deletions.
77 changes: 62 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ 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 . See the [MATLAB Documentation](http://www.mathworks.com/help/matlab/matlab_external/static-path.html). Note that after adding the jar file to the java class path, MATLAB will need to be restarted.
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:
```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.

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.

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

Expand All @@ -26,17 +32,19 @@ onError(obj,conn,message)
onClose(obj,conn,message)
```

`obj` is the object instance of the subclass, it is implicitly passed by MATLAB (see the object-oriented programming documentation of MATLAB).

`message` is the message received by the server.

`conn` is a WebSocketConnection object representing the client connection that caused the event. For example, if a message is received, the `conn` object will represent the client that sent this message. You can send messages to that client through this object.
* `obj` is the object instance of the subclass, it is implicitly passed by MATLAB (see the [object-oriented programming documentation of MATLAB](http://www.mathworks.com/help/matlab/object-oriented-programming.html)).
* `message` is the message received by the server. It will usually be a character array, except for the `onBinaryMessage` method, in which case it will be an `int8` array
* `conn` is a WebSocketConnection object representing the client connection that caused the event. For example, if a message is received, the `conn` object will represent the client that sent this message. You can send messages to that client through this object.

The `WebSocketClient.m` class is very similar to the server, except that no `conn` object is passed to the `onSomething` methods.

These methods will be automatically called when the corresponding event (connection is opened, message received, etc...) occurs. In this way, a reactive behavior can be defined.

The server supports a variety of methods to help talk to clients, look in the MATLAB class file to see what methods are available.

See the `EchoServer.m` and `SimpleClient.m` files in the `examples` folder for an implementation example.
When you are done, do not forget to `delete` the clients and/or servers.

See the `EchoServer.m` and `SimpleClient.m` files in the `examples` folder for an implementation example. A good resource on classes is the [MATLAB object-oriented documentation](http://www.mathworks.com/help/matlab/object-oriented-programming.html).

Example
------
Expand All @@ -50,32 +58,71 @@ to start the server on port 30000.

To test the server, make a client object from the `SimpleClient.m` class:
```matlab
client = SimpleClient('ws://localhost:30000')
>> client = SimpleClient('ws://localhost:30000');
Connected to server at ws://localhost:30000
Client 1063680447 at 127.0.0.1:42520 opened a connection
```

You can now connect and send messages (`client.send('hi!')`). If the server is working properly, you will receive messages identical to the ones you send.

The server can enumerate clients that are connected, just type:
```matlab
server.Connections % you can also view the result as a table: struct2table(server.Connections)
server.Connections % view the result as a table: struct2table(server.Connections)
```

This allows you to send a message to the client via its identifying `HashCode`:
```matlab
>> clientCode = server.Connections(1).HashCode;
>> server.sendTo(clientCode,'hi, this is the server!')
Message received:
hi, this is the server!
```

This allows you to send a message to the client via its identifying HashCode:
The server can be stopped and restarted (this will disconnect clients):
```matlab
clientCode = server.Connections(1).HashCode
server.sendTo(clientCode,'hi, this is the server!')
>> server.stop
Disconnected from server at ws://localhost:30000
```

To close the server, type:
To delete the server, type:
```matlab
stop(server); % or server.stop()
delete(server);
clear server;
```

SSL / WebSocket Secure (wss)
-------

To enable SSL, you must first have a certificate. A self-signed key store can be generated with the java `keytool`, but you should always use a valid certificate in production. From there, open the server by passing the location of the store, the store password, and the key password. With the EchoServer, for example:

```matlab
PORT = 8887; % choose an other port!
STORE = 'C:\keystore.jks';
STOREPASSWORD = 'storepassword';
KEYPASSWORD = 'keypassword';
s = EchoServer(PORT,STORE,STOREPASSWORD,KEYPASSWORD);
```

The client can then connect to it:
```matlab
URI = sprintf(wss://localhost:%d',PORT);
c = SimpleClient(URI,STORE,STOREPASSWORD,KEYPASSWORD);
```

If a valid certificate is used, the default java keystore can be used. For example, we can connect a client directly the the secured [websocket.org](`https://www.websocket.org/echo.html`) test server:

```matlab
>> c2 = SimpleClient('wss://echo.websocket.org');
Connected to server at wss://echo.websocket.org
>> c2.send('hi, this communication is secured!')
Message received:
hi, this communication is secured!
```

Acknowledgments
-------

This work was inspired by a websocket client matlab implementation [matlab-websockets](https://github.com/mingot/matlab-websockets).
This work was inspired by a websocket client MATLAB implementation: [matlab-websockets](https://github.com/mingot/matlab-websockets).

It relies on the [Java-WebSocket](https://github.com/TooTallNate/Java-WebSocket) library.

Expand Down
2 changes: 1 addition & 1 deletion examples/SimpleClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function onTextMessage(obj,message)
function onBinaryMessage(obj,bytearray)
% This function simply displays the message received
fprintf('Binary message received:\n');
display(size(bytearray));
fprintf('Array length: %d\n',length(bytearray));
end

function onError(obj,message)
Expand Down
10 changes: 5 additions & 5 deletions test/runtests.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ function runtests( TEST_SSL )
% Detailed explanation goes here
if nargin==0; TEST_SSL = 0; end

PORT = randi([49152 65535],1);
PROTOCOL = 'ws';
PORT = randi([49152 65535],1);
STORE = fullfile(fileparts(which('WebSocketServer')),'matlab-websocket','keystore.jks');
STOREPASSWORD = 'storepassword';
KEYPASSWORD = 'keypassword';

if TEST_SSL; PROTOCOL = 'wss'; else, PROTOCOL = 'ws'; end
STORE = fullfile(fileparts(which('WebSocketServer')),'matlab-websocket','keystore.jks');
URI = sprintf('%s://localhost:%d',PROTOCOL,PORT);
if TEST_SSL; PROTOCOL = 'wss'; end
URI = sprintf('%s://localhost:%d',PROTOCOL,PORT);

try
% First, create a server
Expand Down

0 comments on commit abfa30d

Please sign in to comment.