diff --git a/examples/chat/cmd_websocket_chat.cpp b/examples/chat/cmd_websocket_chat.cpp index b52d9a19..513b7d8c 100644 --- a/examples/chat/cmd_websocket_chat.cpp +++ b/examples/chat/cmd_websocket_chat.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include "nlohmann/json.hpp" @@ -160,11 +161,9 @@ namespace void interactiveMain() { - std::string user(getenv("USER")); - - WebSocketChat webSocketChat(user); - + std::string user("foo"); std::cout << "Type Ctrl-D to exit prompt..." << std::endl; + WebSocketChat webSocketChat(user); webSocketChat.start(); while (true) @@ -188,6 +187,9 @@ namespace int main() { + std::cout << "main starting" << std::endl; + Socket::init(); + std::cout << "socket initialized" << std::endl; interactiveMain(); return 0; } diff --git a/ixwebsocket/IXSocket.cpp b/ixwebsocket/IXSocket.cpp index 148aa5c5..2aac873f 100644 --- a/ixwebsocket/IXSocket.cpp +++ b/ixwebsocket/IXSocket.cpp @@ -33,6 +33,8 @@ #include #include +#include + // // Linux/Android has a special type of virtual files. select(2) will react // when reading/writing to those files, unlike closing sockets. @@ -155,7 +157,7 @@ namespace ix #ifdef _WIN32 unsigned long nonblocking = 1; - ioctlsocket(fd, FIONBIO, &nonblocking); + ioctlsocket(_sockfd, FIONBIO, &nonblocking); #else fcntl(_sockfd, F_SETFL, O_NONBLOCK); // make socket non blocking #endif @@ -184,7 +186,7 @@ namespace ix #endif int sockfd = _sockfd; - int nfds = std::max(sockfd, _eventfd); + int nfds = (std::max)(sockfd, _eventfd); select(nfds + 1, &rfds, nullptr, nullptr, nullptr); onPollCallback(); @@ -261,7 +263,7 @@ namespace ix flags = MSG_NOSIGNAL; #endif - return (int) ::recv(_sockfd, buffer, length, flags); + return (int) ::recv(_sockfd, (char*) buffer, length, flags); } int Socket::getErrno() const @@ -279,6 +281,18 @@ namespace ix closesocket(fd); #else ::close(fd); +#endif + } + + // FIXME: need finalize + bool Socket::init() + { +#ifdef _WIN32 + INT rc; + WSADATA wsaData; + + rc = WSAStartup(MAKEWORD(2, 2), &wsaData); + return rc != 0; #endif } } diff --git a/ixwebsocket/IXSocket.h b/ixwebsocket/IXSocket.h index b7c9e15e..80486953 100644 --- a/ixwebsocket/IXSocket.h +++ b/ixwebsocket/IXSocket.h @@ -22,6 +22,8 @@ namespace ix Socket(); virtual ~Socket(); + static bool init(); + int hostname_connect(const std::string& hostname, int port, std::string& errMsg); diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 41efedd7..9f02fa90 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -19,8 +19,9 @@ # endif #endif -#include +// #include #include +#include #include #include @@ -272,7 +273,7 @@ namespace ix { { int N = (int) _rxbuf.size(); - ssize_t ret; + int ret; _rxbuf.resize(N + 1500); ret = _socket->recv((char*)&_rxbuf[0] + N, 1500);