Question

I want to learn if MATLAB can talk to a websocket. If so, which library of MATLAB should I use? I need to use this information in my project in which I communicate between ROS and MATLAB through rosbridge ( a websocket server connected to ROS ). Any help will be greately appreciated.

For example for C, its like this:

  struct addrinfo *address;
  getaddrinfo("192.168.0.111", "9090", NULL, NULL);

  int sockfd = socket(PF_INET, SOCK_STREAM, 0);
  connect(sockfd, address->ai_addr, address->ai_addrlen);

  char* message = "raw\r\n\r\n";
  send(sockfd, message, strlen(message));
Was it helpful?

Solution

From that code, it looks like a regular TCP connection. "websocket" means something different. For TCP connections, use the free TCP/UDP/IP toolbox found here:

http://www.mathworks.com/matlabcentral/fileexchange/345-tcpudpip-toolbox-2-0-6

It is at least as good as the official network tools provided in MATLAB's Instrument Control Toolbox.

Equivalent code will look something like this:

con=pnet('tcpconnect','192.168.0.111', 9090); 
pnet(con,'printf','raw\r\n\r\n'); 
pnet(con,'close');

OTHER TIPS

I know it is an old question but my answer will probably help someone else in the future (I hope).

With Matlab 2015a, in SIMULINK there is the Robotics Toolbox in which you can basically use ROS without any difficulty. You can subscribe/publish to a ROS topic without any problem.

Yes it can.

Matlab library backed by a Java WebSocket library

On GitHub there is a MatlabWebSocket project that pretty much just works. Using the included example code you can make the connection and the send/receive messages over a WebSocket. The MatlabWebSocket code relies on the Java-WebSocket library and provides the necessary callback code.

I'm using this library and the performance is great.

Super simple Matlab client example

server = 'ws://echo.websocket.org';
client = SimpleClient(server);
client.send(char('Hello, world!'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top