void connect(const std::string& host, int port) {
int sock;
bool connected;
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if(sock == -1) { Error("connect(): failed to create socket"); return; } fcntl(sock, F_SETFL, O_NONBLOCK);
struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); if(inet_pton(AF_INET, host.c_str(), &addr.sin_addr) != 1) { Error("connect(): inet_pton failed"); return; }
const int res = ::connect(sock, (struct sockaddr *)&addr, sizeof(addr)); if(res == -1) { if(errno == EINPROGRESS) { Debug("connect(): Connection in progress");
/* Wait for connection to complete */ fd_set sockets; FD_ZERO(&sockets); FD_SET(_data->socket, &sockets);
/* You should probably do other work instead of busy waiting on this... or set a timeout or something */ while(select(sock + 1, nullptr, &sockets, nullptr, nullptr) <= 0) { Debug(std::to_string(errno)); }
connected = true;
} else { Error("Socket::connect(): connection failed"); return; } } else { connected = true; }}void netClient(){ bool connected = false; int sock;
// create socket sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock == -1) printf("SOCKERR\n");
fcntl(sock, F_SETFL, O_NONBLOCK);
std::string hostName = "127.0.0.1"; const uint16_t port = 2320;
sockaddr_in addr; // Memory::Clear(&addr, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); uint32_t ipAddr = 0;
ipAddr = inet_addr(hostName.c_str());
addr.sin_addr.s_addr = ipAddr; printf("Connecting to '%s' => '%d.%d.%d.%d', port %d\n", hostName.c_str(), ipAddr & 0xFF, (ipAddr >> 8) & 0xFF, (ipAddr >> 16) & 0xFF, (ipAddr >> 24) & 0xFF, port);
const int connectRes = connect(sock, (sockaddr*)&addr, sizeof(addr)); if (-1 == connectRes) { if (((errno == EINPROGRESS) || (errno == EWOULDBLOCK) || (errno == EISCONN))) { connected = true; } } if (!connected) printf("Failed to connect to '%s:%d'\n", hostName.c_str(), port);
fd_set fdr, fdw; FD_ZERO(&fdr); FD_ZERO(&fdw); FD_SET(sock, &fdr); FD_SET(sock, &fdw);
// for windows, it is necessary to provide an empty timeout // structure, in order for select() to not block struct timeval tv = { }; const int selectRes = select(int(sock+1), &fdr, &fdw, 0, &tv); if (selectRes == -1) { printf("select() failed.\n"); } else if (selectRes > 0) { printf("Try\n"); if (FD_ISSET(sock, &fdr)) { printf("Should Recv OK!\n"); } if (FD_ISSET(sock, &fdw)) { printf("Should Send OK!\n"); } } printf("selectedRes: %d\n", selectedRes);}Connecting to '127.0.0.1' => '127.0.0.1', port 2320
selectedRes: 0
WebSocket connection to 'ws://127.0.0.1:2320/' failed: Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE
WebSocket connection to 'ws://127.0.0.1/' failed: Error during WebSocket handshake: Invalid status line
WebSocket connection to 'ws://127.0.0.1:2321/' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received
"The page was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint. This request has been blocked; this endpoint must be available over WSS."
I read that I can turn regular sockets into secure sockets with OpenSSL, but I am unable to build OpenSSL for Emscripten.
See here: https://github.com/kripken/emscripten/issues/6970