Search code examples
cmatlabwebsocketros

Can MATLAB talk to a websocket?


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));

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');