0

As the title says how can I save in string the result of the executed command?

SendCommand("server.hostname");

My code:

public void SendCommand(string command)
{
    PacketModel packet = new PacketModel()
    {
        Identifier = 1,
        Message = command,
        Name = "RustManager"
    };
    string packetString = JsonConvert.SerializeObject(packet);
    _webSocket.SendAsync(packetString, null);
}

public void GetServerHostname()
{
    SendCommand("server.hostname");
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
P.Dan
  • 37
  • 6

1 Answers1

3

Due to my small reputation I cannot comment - which is what I would have done before that.

Normally methods that end on Async are async and return a Task<T> type. Using the await keyword makes your method async which is why you have to mark it as async in the method head. Link to C#-Documentation on the await keyword

It is really hard to say how to get your code running since I don't have alot of information but maybe this helps:

public async void SendCommand(string command)
{
   PacketModel packet = new PacketModel()
   {
     Identifier = 1,
     Message = command,
     Name = "RustManager"
   };
   string packetString = JsonConvert.SerializeObject(packet);
   var result = await _webSocket.SendAsync(packetString, null);
}

EDIT 1: After getting some new information here is my new answer: You use this class for your websocket. If you look at the signiture of the "SendAsync" method you can see, that it returns void (which means "nothing"). So you will not be able to "Store some kind of information" here. The method looks like this:

public void SendAsync (string data, Action<bool> completed) { [...] }

You will have to listen to the WebSocket and wait for a server-side response. It seems, that the library supports that via events:

ws.OnMessage += (sender, e) => { ... };

So you can define an eventhandler to process the server-response.

If you would like to get the message data, you should access e.Data or e.RawData property. e.Data property returns a string, so it is mainly used to get the text message data. (source (GitHub Readme))

So to fullfil your wishes try the following:

1.) At initialization of your _websocket instance subscribe to the .OnMessageevent with a corresponding event handler. (Some information about that)

2.) Send your message as you do it now with SendAsync

3.) If your server responds with a message to the network socket, the OnMessageevent will fire and you will be able to get the Data from the Eventargument e

(I did not test this - but it should work since it is used this way in the examples)

Luchspeter
  • 744
  • 7
  • 24
  • When I use it your code it says operator await can not be used for void... – P.Dan Jan 31 '19 at 01:47
  • Well I need more information about your code. What type is `_websocket` and so on? – Luchspeter Jan 31 '19 at 19:31
  • I am using this websocket https://github.com/PsychoTea/RustManager same as that What I am trying to do is to save command which I executed and convert it and save it to string or something... – P.Dan Jan 31 '19 at 20:55
  • I would appreciate your help. it is in folder RustManager->ServerManagement-> WebSocketConnection.cs Using the websocket-sharp.dll – P.Dan Jan 31 '19 at 21:29
  • Ahh I see. Thank you for the information. I eddited my answer. – Luchspeter Feb 01 '19 at 15:11
  • Sure, I have one more question. How to save this numbers to a string "55 FPS" how to save it to a string the 55 number it is a variable? – P.Dan Feb 02 '19 at 14:18
  • Well there are multiple ways to do that. If you know that you string *allways* looks exactly like that, you can do String-Splitting and then Convert the String into an Int. ` string fps_string = InputString.Split(' ')[0]; int fps = Int32.Parse(fps_string); ` But be careful here. Look at this [link](https://stackoverflow.com/questions/8928601/how-can-i-split-a-string-with-a-string-delimiter) and this [link](https://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int) – Luchspeter Feb 02 '19 at 14:39
  • Ok thanks. I have one more question and I think it is my last question. How can I send public string Players; from WebSocketConnection.cs to MainForm.cs – P.Dan Feb 03 '19 at 17:43
  • Well there are multiple ways to do that. I think the best way would be to let the MainForm have a reference to the WebSocketConnection object and manage the WebSocket object from there. Then it can access all its public members. Another way would be to use events. You could subscribe to some custom event in the websocket class that gets invoked when recieving data. – Luchspeter Mar 01 '19 at 07:56