websocket 使用socket.ioVB.NET连接到www.example.com

dzhpxtsq  于 2023-06-06  发布在  .NET
关注(0)|答案(1)|浏览(282)

我有一个在Heroku上运行的应用程序,它使用socket.io与客户端通信。到目前为止,所有的客户端都是HTML/JS,所以工作得很好。现在,我想用Visual Basic编写一个客户端,但我找不到任何其他人编写的库来socket.io从VB.NET与www.example.com进行对话。
有没有这样一个图书馆,我只是没有找到?如果没有,在VB.NET中实现WebSockets或长轮询会有多难?只包含一个WebBrowser控件(使用标准的socket.io库)来与服务器通信并在其中传递数据是否值得?
我已经有几年没有在VB中做过任何工作了,所以如果我在这里遗漏了一些明显的东西,我很抱歉。

nlejzf6q

nlejzf6q1#

我也有类似的问题。我通过在node.js中使用net和在VB.Net端使用TcpClient解决了我的问题。

Node.js端

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端

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

相关问题