question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebSocketClient's KeepAliveInterval can't be disabled.

See original GitHub issue

In console window (OnLogMessage from DiscordBot): [Gateway] Disconnected: Received close code 4002: Error while decoding payload.

In Visual Studio output window:

Exception thrown: 'Discord.Net.WebSocketException' in Discord.Net.dll
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.ni.dll
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.ni.dll
Exception thrown: 'Discord.Net.WebSocketException' in mscorlib.ni.dll

This occurs every 32.5 seconds when there is absolutely no activity in connected channels. With activity, this does not occur.

After disconnecting, Discord.Net reconnects and, if there is still no activity, it disconnects again after 32.5 seconds.

Guessing it has something to do with the heartbeat.

Using .Net Core RC2. Latest master branch from GitHub.

project.json:

{
    "version": "1.0.0-*",
    "buildOptions": {
        "emitEntryPoint": true
    },

    "dependencies": {
        "Newtonsoft.Json": "8.0.4-beta1",
        "Discord.Net": "0.9.2"
    },

    "frameworks": {
        "netstandard1.5": {
            "imports": [
                "portable-net451+win81"
            ]
        }
    },

    "runtimes": {
        "win81-x64": { }
    }
}

Program.cs:

using System;
using System.Text;
using Discord;

namespace InactivityIssue
{
    public class Program
    {
        public static void Main(string[] args) => new Program().Start();

        private DiscordClient _client;

        public void Start()
        {
            _client = new DiscordClient(x =>
            {
                x.LogLevel = LogSeverity.Debug;
                x.LogHandler = OnLogMessage;
            });

            _client.ExecuteAndWait(async () =>
            {
                await _client.Connect("BOTTOKEN");
            });
        }

        private void OnLogMessage(object sender, LogMessageEventArgs e)
        {
            //Color
            ConsoleColor color;
            switch (e.Severity)
            {
                case LogSeverity.Error: color = ConsoleColor.Red; break;
                case LogSeverity.Warning: color = ConsoleColor.Yellow; break;
                case LogSeverity.Info: color = ConsoleColor.White; break;
                case LogSeverity.Verbose: color = ConsoleColor.Gray; break;
                case LogSeverity.Debug: default: color = ConsoleColor.DarkGray; break;
            }

            //Exception
            string exMessage;
            Exception ex = e.Exception;
            if (ex != null)
            {
                while (ex is AggregateException && ex.InnerException != null)
                    ex = ex.InnerException;
                exMessage = ex.Message;
            }
            else
                exMessage = null;

            //Source
            string sourceName = e.Source?.ToString();

            //Text
            string text;
            if (e.Message == null)
            {
                text = exMessage ?? "";
                exMessage = null;
            }
            else
                text = e.Message;

            //Build message
            StringBuilder builder = new StringBuilder(text.Length + (sourceName?.Length ?? 0) + (exMessage?.Length ?? 0) + 5);
            if (sourceName != null)
            {
                builder.Append('[');
                builder.Append(sourceName);
                builder.Append("] ");
            }
            for (int i = 0; i < text.Length; i++)
            {
                //Strip control chars
                char c = text[i];
                if (!char.IsControl(c))
                    builder.Append(c);
            }
            if (exMessage != null)
            {
                builder.Append(": ");
                builder.Append(exMessage);
            }

            text = builder.ToString();
            Console.ForegroundColor = color;
            Console.WriteLine(text);
        }
    }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Nyatracommented, Jul 17, 2016

The way I worked around the issue on Windows for now, is by setting the global Keep-alive Interval in the registry to something really large.

HKLM:\Software\Microsoft\WebSocket\KeepaliveInterval

It’s should be a DWORD, if it does not exist already.

This will affect anything that uses the global setting and does not have it’s own setting. (All .NET Core WebSockets currently)

I found this in an msdn page:

WINHTTP_OPTION_WEB_SOCKET_KEEPALIVE_INTERVAL

Sets the interval, in milliseconds, to send a keep-alive packet over the connection. The default interval is 30000 (30 seconds). The minimum interval is 15000 (15 seconds). Using WinHttpSetOption to set a value lower than 15000 will return with ERROR_INVALID_PARAMETER.

Note The default value for WINHTTP_OPTION_WEB_SOCKET_KEEPALIVE_INTERVAL is read from HKLM:\SOFTWARE\Microsoft\WebSocket\KeepaliveInterval. If a value is not set, the default value of 30000 will be used. It is not possible to have a lower keepalive interval than 15000 milliseconds.

0reactions
Auralyticalcommented, Jul 22, 2016

This fix is live.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebSocketClient's KeepAliveInterval can't be disabled. #60
The way I worked around the issue on Windows for now, is by setting the global Keep-alive Interval in the registry to something...
Read more >
c# - .NET WebSockets forcibly closed despite keep-alive ...
We have written a simple WebSocket client using System.Net.WebSockets. The KeepAliveInterval on the ClientWebSocket is set to 30 seconds.
Read more >
WebSockets support in ASP.NET Core
This article explains how to get started with WebSockets in ASP.NET Core. WebSocket (RFC 6455) is a protocol that enables two-way persistent ...
Read more >
Client WebSocket disconnects when browser is minimized ...
1. disable the feature "Intensive throttling of Javascript timer wake ups" in Chrome or, 2. change the web-transport inactive-timeout in the ...
Read more >
API Gateway websockets don't stay alive
My websocket connection works fine until the keepalive timeout triggers on the client, and then it closes despite messages being sent successfully in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found