2

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?

4

2 回答 2

1

我遇到了同样的问题 - 只需更换

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

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

为我解决了这个问题。

于 2015-03-19T18:11:15.417 回答
0

在 C# 控制台应用程序中,我使用以下命令将客户端连接到服务器:

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

您上面的代码正在使用连接

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

可能有两个问题之一 -

  1. 首先是查看 WebSocketClient 是否可以代替。
  2. 确保您的 url 格式为 ws://ur:port/context。这让我有一段时间不高兴。
于 2013-10-21T05:52:24.413 回答