Marek M.
Marek M.

Reputation: 3951

WebSockets in firefox

For implementing my websocket server in C# I'm using Alchemy framework. I'm stuck with this issue. In the method OnReceive when I try to deserialize json object, I get a FormatException: "Incorrect format of the input string." (maybe it's different in english, but I'm getting a localized exception message and that's my translation :P). What is odd about this is that when I print out the context.DataFrame I get: 111872281.1341000479.1335108793.1335108793.1335108793.1; __ad which is a substring of the cookies sent by the browser: __gutp=entrystamp%3D1288455757%7Csid%3D65a51a83cbf86945d0fd994e15eb94f9%7Cstamp%3D1288456520%7Contime%3D155; __utma=111872281.1341000479.1335108793.1335108793.1335108793.1; __adtaily_ui=cupIiq90q9.

JS code:

// I'm really not doing anything more than this
var ws = new WebSocket("ws://localhost:8080");

C# code:

static void Main(string[] args) {
    int port = 8080;

    WebSocketServer wsServer = new WebSocketServer(port, IPAddress.Any) {
        OnReceive = OnReceive,
        OnSend = OnSend,
        OnConnect = OnConnect,
        OnConnected = OnConnected,
        OnDisconnect = OnDisconnect,
        TimeOut = new TimeSpan(0, 5, 0)
    };

    wsServer.Start();

    Console.WriteLine("Server started listening on port: " + port + "...");

    string command = string.Empty;

    while (command != "exit") {
        command = Console.ReadLine();
    }

    Console.WriteLine("Server stopped listening on port: " + port + "...");

    wsServer.Stop();

    Console.WriteLine("Server exits...");
}

public static void OnReceive(UserContext context) {
    string json = "";
    dynamic obj;

    try {
        json = context.DataFrame.ToString();
        Console.WriteLine(json);
        obj = JsonConvert.DeserializeObject(json);
    } catch (Exception e) {
        Console.WriteLine(e.Message);
        Console.WriteLine(e.StackTrace);

        return;
    }
}

On the C# side I'm using Newtonsoft.Json, though it's not a problem with this library...

EDIT: One more thing - I browsed through the code in here: https://github.com/Olivine-Labs/Alchemy-Websockets-Example and found nothing - I mean, I'm doing everything the same way authors did in this tutorial...

EDIT: I was testing the above code in Firefox v 17.0.1, and it didn't work, so I tested it under google chrome, and it works. So let me rephrase the question - what changes can be made in js, so that firefox would not send aforementioned string?

Upvotes: 2

Views: 610

Answers (2)

kogh
kogh

Reputation: 1015

I ran into the same issue - simply replacing

var ws = new WebSocket("ws://localhost:8080");

with

var ws = new WebSocket("ws://127.0.0.1:8080");

fixed the issue for me.

Upvotes: 1

user2537701
user2537701

Reputation:

In C# console app I connect the client to the server using :

var aClient = new WebSocketClient(@"ws://127.0.0.1:81/beef");

Your code above is connecting using

var ws = new WebSocket("ws://localhost:8080");

There could be one of two issues -

  1. First is to see if WebSocketClient works instead.
  2. To make sure your url is of the format ws://ur:port/context. This threw me off for a while.

Upvotes: 0

Related Questions