Dan Hlavenka
Dan Hlavenka

Reputation: 3817

Connect to socket.io using VB.NET

I have an application running on Heroku that uses socket.io to communicate with clients. So far, all the clients have been HTML/JS, so that's been working great. Now I want to write a client in Visual Basic, and I can't find any libraries anyone else has written to talk to socket.io from VB.NET.

Is there such a library that I just haven't found? If not, how hard would it be to implement WebSockets or long polling in VB.NET? Would it be worthwhile to just include a WebBrowser control (using the standard socket.io library) to talk to the server and pass data in and out of it?

It's been a few years since I did any work in VB, so I apologize if there's something obvious I'm missing here.

Upvotes: 3

Views: 1955

Answers (1)

Mehmed Fatih Temiz
Mehmed Fatih Temiz

Reputation: 21

I have similar issue. I fixed my issue by using net in node.js and TcpClient in VB.Net side.

Node.js side

const net = require('net');

const server = net.createServer((socket) => {
  console.log('Client connected');

  socket.on('data', (data) => {
    console.log(`Received data: ${data}`);

    // Process the received data as needed

    // Send response back to the client
    socket.write('Response from Node.js');
  });

  socket.on('end', () => {
    console.log('Client disconnected');
  });
});

const port = 3000;
server.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});

Visual basic side

        Try
        While True
            Dim client As TcpClient = New TcpClient()
            client.Connect("localhost", 3000) ' Replace with the actual server IP and port

            Dim data As Byte() = Encoding.ASCII.GetBytes("Data from VB.NET")
            Dim stream As NetworkStream = client.GetStream()
            stream.Write(data, 0, data.Length)

            Dim response As Byte() = New Byte(1023) {}
            Dim bytesRead As Integer
            Dim responseData As String = ""

            ' Read data until no more available
            Do
                bytesRead = stream.Read(response, 0, response.Length)
                responseData += Encoding.ASCII.GetString(response, 0, bytesRead)
            Loop While stream.DataAvailable

            stream.Close()
            client.Close()
        End While
    Catch ex As Exception

    End Try

Upvotes: 1

Related Questions