Skip to content

No Close event for IXWebSocket client (11.4.2) when server disconnects #396

@mwinch2

Description

@mwinch2

When using version 11.4.2 an IXWebSocket client does not get a Close notification when the server disconnects.
IXWebSocket was built via vcpkg, with triplet x64-windows-static, using VS2015.

I have built a simple client based on the example code. The code is shown below.
I had version 7.9.2 available from an older project and tried that, and it worked fine.

For the server I am using a simple nodejs script which just echoes anything received.

As a workaround I have set a short ping time - the Close notification is generated when the ping fails after the server has
disconnected.
Let me know if you need any more info.

// IXWebsocketClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <ixwebsocket/IXWebSocket.h>
#include <ixwebsocket\IXNetSystem.h>

#include <conio.h>
#include <windows.h>

int main()
{
	ix::initNetSystem();

	// Our websocket object
	ix::WebSocket webSocket;  

	std::string url("ws://localhost:8088/");
	webSocket.setUrl(url);

	// Optional heart beat, sent every 45 seconds when there is not any traffic
	// to make sure that load balancers do not kill an idle connection.
	webSocket.setPingInterval(45);

	// Per message deflate connection is enabled by default. You can tweak its parameters or disable it
	webSocket.disablePerMessageDeflate();

	bool connected = false;

	// Setup a callback to be fired when a message or an event (open, close, error) is received
	webSocket.setOnMessageCallback([&](const ix::WebSocketMessagePtr& msg)
	{
		if (msg->type == ix::WebSocketMessageType::Message)
		{
			std::cout << msg->str << std::endl;
		}
		else if (msg->type == ix::WebSocketMessageType::Open)
		{
			std::cout << "connected" << std::endl;
			connected = true;
		}
		else if (msg->type == ix::WebSocketMessageType::Close)
		{
			std::cout << "disconnected" << std::endl;

			// The server can send an explicit code and reason for closing.
			// This data can be accessed through the closeInfo object.
			std::cout << msg->closeInfo.code << std::endl;
			std::cout << msg->closeInfo.reason << std::endl;
		}
	}
	);

	// Now that our callback is setup, we can start our background thread and receive messages
	webSocket.start();

	while (!connected)
	{
		Sleep(100);
	}

	// Send a message to the server (default to TEXT mode)
	webSocket.send("hello world");

	// The message can be sent in BINARY mode (useful if you send MsgPack data for example)
	//webSocket.sendBinary("some serialized binary data");

	std::cout << "Press any key to continue...";
	_getch();

	// Stop the connection
	webSocket.stop();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions