3

I have a C# class to manage a connection between a server and a win forms app. This class sets up a message queue and sends and receives data across the Socket. My original intention was to use the same class on both the client and the server to manage messaging. However on the server the Web Socket object is a System.net.websocket and on the client it is a ClienWebSocket that extends WebSocket. I have tried just casting the WebSocket object as a ClientWebSockets because one inherits the other however this is not working. Is there anyway I can get a common object type for WebSockets that will work on both sides of the connection?

Thanks

Lightning77
  • 313
  • 5
  • 14
  • 1
    It doesn't matter that different types are used in the code on each side of the connection (in the client / server), as long as they can communicate using the same protocol. – user2864740 Sep 13 '16 at 20:16
  • Yes, however I am passing in the object that represents the connection into this class (trying to use one class for both sides) but the connection is repeated by different objects on either side. Thats my problem – Lightning77 Sep 13 '16 at 20:22
  • Basically if I make the class accept a websocketclient object then I can't pass in a websocket object, if I change it the other way then it's the same problem in reverse – Lightning77 Sep 13 '16 at 20:24
  • An object can only be cast into a type it implements. Not all WebSocket's implemented ClientWebSocket, but all ClientWebSocket's implement WebSocket. Because of this, a explicit downcast (which may fail at runtime) is required - if there is an InvalidCastException this is the problem, and the exact type which cannot be cast will be included in the error message. A ClientWebSocket typed expression is allowed wherever a WebSocket expression is required, as an upcast will be automatically performed (because ClientWebSocket is-a WebSocket). – user2864740 Sep 13 '16 at 20:25
  • Ok, so I had it reverse I should be allowed to cast the "clientwebsocket" into a "websocket" – Lightning77 Sep 13 '16 at 20:30

0 Answers0