Anybody familiar with c# web sockets?



  • I need to make a c# websocket client to connect to hitbox.tv, but literally every sample I've seen so far is either on how to create a web socket server, or how to connect with a c# client - but the sample doesn't work.

    I've tried alchemy, superwebsockets, owin, and sockets.io, and I just don't fucking get how to do it.

    The concept is extremely basic:

    1. Query a remote site for IPs
    2. Use retrieved IPs to issue a second request to ws://ip
    3. Send/receive as normal

    http://help.hitbox.tv/customer/portal/articles/1470464-changes-to-websocket---3-6-2014

    I've gotten as far as getting the token, but now I can't do anything with it.

    I guess some code could help to get to the 'websocket' part:

    var hitbox = new HitBoxWebSocket();
    
    public class HitBoxWebSocket
        {
            private static WebClient hitboxWebClient = new WebClient();
            private static int connectServer = 0;
            private List<HitboxServerData> hitboxServerData = new List<HitboxServerData>(); 
    
            struct HitboxServerData
            {
                public string server_id;
                public string server_type;
                public string server_name;
                public string server_host_name;
                public string server_location;
                public string server_ip;
                public string server_internal_ip;
                public string server_load;
                public string server_enabled;
                public string server_updated;
            }
    
            public HitBoxWebSocket()
            {
                hitboxWebClient.Proxy = null;
                hitboxServerData = JsonConvert.DeserializeObject<List<HitboxServerData>>(hitboxWebClient.DownloadString(@"http://api.hitbox.tv/chat/servers"));
                ConnectHitboxServer(0);
            }
    
            public static void HitboxCleanup()
            {
                hitboxWebClient.Dispose();
            }
    
            private void ConnectHitboxServer(int server)
            {
                connectServer = server;
                try
                {
                    var hitboxJsonServerData = hitboxWebClient.DownloadString(string.Format(@"http://{0}/socket.io/1/", hitboxServerData[connectServer].server_ip));
                    var connectionId = hitboxJsonServerData.Split(':')[0];
                    Console.WriteLine(connectionId);
                }
                catch (Exception ex)
                {
                    if (connectServer < hitboxServerData.Count)
                    {
                        ConnectHitboxServer(++connectServer);
                    }
                    else
                    {
                        LogItem.WriteLogFile(@"Hitbox", @"Connecting", 4, "Connection failed", "ConnectHitboxServer");
                        throw;
                    }
                }
            }
        }
    


  • I've done this before, let me see if I can dig out some code.

    EDIT: Goddamned, I didn't save it. Sorry guys.

    I can say this:

    1. A lot of sites require the use of the wss protocol instead of ws
    2. A lot of sites require that you send a JSON object to authenticate within X milliseconds of opening the stream, or it'll immediately be closed again
    3. No goddamned web sockets implementation works like any other anywhere in the world ever. The one I was working with was Webtrends' streaming analytics data feature, even if I had the working code for that, there's no guarantee it'd work for Hitbox.tv.

Log in to reply