0

I am porting some code from QT in .NET. I am using VS2019 Pro and need to capture WebSocketClient events as a message received.

I have this:

using System;
using System.Linq;
using System.Net.WebSockets;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DevWebSockets{
    public delegate void OnDev_TxtMsgRcvd(object sender,EventArgs e);

    class Program {

        public enum WebSocketConnectionState {
            SockNotConnected, SockEstablishingConnection, SockConnected
        }

        public WebSocketConnectionState m_ConnectionState;

        public event OnDev_TxtMsgRcvd DevTxtRcvdEvent; // OnTxtMsgRcvd;

        public void OnDev_TxtMsgRcvd(object sender, EventArgs e) {
            Console.WriteLine("Dev - Rcvd Text message");
        }

        static void Main(string[] args) {

            try {
                new Program().Start().Wait();
            } catch (Exception ex) {
                // Console.WriteLine("Exception {0} - {1}", ex.Message, ex.InnerException);
            }
        }


        public async TaskStart(){          
            m_ConnectionState = WebSocketConnectionState.SockNotConnected;

            Console.WriteLine("Press enter to start Please ");
            string name = Console.ReadLine();

            Console.WriteLine("Connecting...");


            var cts = new CancellationTokenSource();
            var CWS = new ClientWebSocket();

            CWS += new EventHandler<ClientWebSocket>(OnDev_TxtMsgRcvd(Object s, EventArgs e));
        }
    }

}

At this point the compiler has placed a

CS1525 - Invalid expression term 'object' for the word Object
CS1003 - Syntax error ',' expected under the letter s
CS1003 - Syntax error ',' expected under the letter e

Hovering on the red squiggly lines produces: object is a type which is not valid in the given context

  • The one under 's' says: The name 's' does not exist in the current context
  • The one under EventArgs is the same as the one under object
  • The one under 'e' is the same as the one under 's'

Basically I want to have a method to catch messages incoming from the device so I can determine actions to be taken if any.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Tleilax
  • 77
  • 10
  • Alex, there is no 'even_name' member in the ClientWebSocket class – Tleilax Apr 03 '20 at 14:24
  • Alex; I thought the idea to create an event and a handler was to implement events that are not necessarily in the base class and create a method to handle the specific new event. Am I wrong? I looked at this and thought it could: https://stackoverflow.com/questions/85137/how-to-add-an-event-to-a-class#85188 – Tleilax Apr 03 '20 at 17:34
  • C# does not have the features you seem to think it does. First of all, to subscribe to an event, you _must_ specify the name of the `event` member found in the type. Trying to subscribe to an event directly on an instance of of some class, as your code does here, makes no sense in C#. Secondly, you cannot "invent" events that aren't there. Even if you provide the name of an event on the `CWS` object, it would have to be an event that actually exists. The only way to "add" an event to a type is to inherit that type and declare it in that new subclass. – Peter Duniho Apr 04 '20 at 00:33
  • Note that the answer you reference is putting the newly declared event in the actual class where it's desired. You have to be able to modify the source code in the class to do that. It's not something you can do arbitrarily to any type you just happen to come across. – Peter Duniho Apr 04 '20 at 00:35
  • Just to be picky the use of the `On*` naming convention here is non-standard. It is typical that class may expose events, such as `Click`, and these events are not named `OnClick` and neither are the methods that handle these events. The method called `OnClick` is typically a `protected virtual` method in the class that defines the `Click` event to enable subclasses to override the raising of the `Click` event. Handlers of events are typically called things like `Button1_Click` (the name of the instance and the event that you're handling). – Enigmativity Apr 04 '20 at 01:14
  • Thank you all for the comments and insights. As Enigmativity mentioned, I nay need to get back to the father class. I will look into this starting in the morning. I'll be back if needed but this is a great community; Thank you! – Tleilax Apr 06 '20 at 00:46

0 Answers0