简体   繁体   English

如何将 HTML WebSocket 连接到 C++ Socket

[英]How to connect HTML WebSocket to C++ Socket

I have an app where I am trying to connect to a C++ server which opens up a socket.我有一个应用程序,我试图连接到打开套接字的 C++ 服务器。

// Client side // 客户端

var ws = new WebSocket('ws://<<IP:PORT>>');

In the server side, it is opening up a socket using在服务器端,它使用打开一个套接字

int sockfd = socket(domain, type, protocol) 

(ref: https://www.geeksforgeeks.org/socket-programming-cc/ ) (参考: https : //www.geeksforgeeks.org/socket-programming-cc/

Not aware of the c++ server implementation.不知道 C++ 服务器实现。

I was told that the server would start a WebSocket server to which my HTML app can connect.我被告知服务器将启动一个 WebSocket 服务器,我的 HTML 应用程序可以连接到该服务器。

But, the connection is not happening.但是,连接没有发生。 I feel that the WebSocket on the client side is not same as socket on the server side and hence the connection is not establishing.我觉得客户端的WebSocket与服务器端的套接字不同,因此没有建立连接。

Please, someone, suggest what is wrong and how to make it right.请有人提出问题以及如何改正。

PS Please don't mind if it is a dumb question. PS请不要介意这是一个愚蠢的问题。

Since the server guys are not ready to change their implementation of the plain socket, as a workaround I created a C++ client which connects to the server using the plain socket.由于服务器人员还没有准备好更改他们对普通套接字的实现,作为一种解决方法,我创建了一个 C++ 客户端,它使用普通套接字连接到服务器。 Then I am creating a WebSocket Server using NodeJs which executes the C++ client as a child process to get the data.然后我使用 NodeJs 创建一个 WebSocket 服务器,它将 C++ 客户端作为子进程执行以获取数据。 So, my WebSocket client is now able to get the data from the main server through the NodeJs WebSocket interface server.因此,我的 WebSocket 客户端现在可以通过 NodeJs WebSocket 接口服务器从主服务器获取数据。

Cons:缺点:

  1. A dependency of the C++ client. C++ 客户端的依赖项。
  2. Latency ( since network speed is not a factor now, it works ).延迟(因为网络速度现在不是一个因素,它可以工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM