I'm setting up a Websocket in C# and I'm having trouble understanding the client/server cross-solution communication. I've created a solution (.sol) for the client and a solution for the server. "WebSocketSharp" and "Newtonsoft.Json" are two libraries that I'm using. I have experience with C# (VS) but not with Websockets or Serialization. Would greatly appreciate any help. Ok, about the problem ...

The application is hosting an Auction, which communicates product and bid information with clients, while listening for client bid requests. The server broadcasts product data to clients, and the Server defines the Product object in a Product.cs file BUT since both solutions are independent, I don't know how to let them share a definition without redundantly defining a "Product.cs" file in the Server.sol solution and a "Product.cs" file in the Client.sol solution.

I'm wondering if we can clean this up with a cross-solution reference or something. Perhaps the server can convey this datatype directly to the client somehow, but I'm not aware of such a strategy.

Here's an example ...

    public void BroadcastNewProduct(Product product) // "lets broadcast this new product that is being auctioned"
        Sessions?.Broadcast(JsonConvert.SerializeObject(new Tuple<string, Product>("S_NewProductAlert", product))); // "we will clarify our message type with a tuple"
    public void ReceiveNewProductMessage(void message)
        Tuple<string, object> tuple = JsonConvert.DeserializeObject<Tuple<string, object>>(message); // "lets deserialize"
        switch(tuple.item1) // "lets see what type of message this is, by evaluating the header"
            case "S_NewProductAlert": // "looks like the object is a new Product"
                productList.Add((Product)tuple.Item2);  // "so lets cast .. but wait, this requires a local Product definition too (which is redundant)"
            // "the server already has a definition for Product, but since it is defined in a separate solution, I can't access it!"
        // ... 
    } // Thanks for any help in advance :)

I also feel awkward about the "Tuple" means of communication between client and server, so I'd appreciate any datatype suggestions as well. Encapsulating a GetType() or inheriting from an abstract "Message" class are ideas that I've considered, but the overarching issue is sharing non-primitive datatypes. Thanks!!

0 Answers