My goal is to get information about the incoming donate through sockets: name, amount

The network has the following article which briefly describes the interaction with donationalerts in JavaScript

Below is the code from the article.

var socket = io("socket.donationalerts.ru:3001"); socket.emit('add-user', {token: "Ваш токен", type: "minor"}); socket.on('donation', function(msg){ // Ваша функция, обрабатывающая донат }); 

I am trying to convert the code under C #. SocketIO4Net.Client library is used for interaction .

However, I don’t know how to convert the following part of the { token: "my-token", type: "minor"} type to dynamic? The Emit method Emit input (string eventName, dynamic payload) , how do braces turn into dynamic ? There is an assumption that this is JSON

  socket = new Client("socket.donationalerts.ru:3001"); socket.Emit("add-user", { token: "12345", type: "minor"}); socket.On("donation", (data) => { // Ваша функция, обрабатывающая донат var test = data; }); 
  • This is Typle, unnamed struct, VS which version? - NewView 4:23 pm
  • 2
    But for a moment, JavaScript is used in the article, in C# it is not paste-paste. - NewView
  • @NewView oh, you're right, my carelessness - StriBog

1 answer 1

The code you copied is JavaScript code, so declaring a structure in C# will not work. For serialization, you need to prepare your own structure, although you can use the Typle type, but this is a topic for a separate issue.

Example structure:

 using Newtonsoft.Json; namespace TestNameSpace { [JsonObject(MemberSerialization.OptIn)] public class MyRequest { [JsonProperty("token")] public string MyToken { get; set; } [JsonProperty("type")] public string MyType { get; set; } public string ToJsonString() { return JsonConvert.SerializeObject(this); } public static MyRequest Deserialize(string jsonString) { return JsonConvert.DeserializeObject<MyRequest>(jsonString); } } } 

Respectively final code:

 socket.On("connect", (fn) => { MyRequest mr = new MyRequest() { MyToken = "12345", MyType = "minor"}; socket.Emit("add-user", mr); }); socket.On("update", (data) => { MyResponse mrs = data.Json.GetFirstArgAs<MyResponse>(); Console.WriteLine("Type: {0}\r\n", mrs.MyField); }); socket.Connect(); 
  • Made, compiled, but there is no reaction to events :( Can you suggest any way to check the code, in terms of whether it can receive a message from at least some server? - StriBog
  • one
    There, the syntax of the event tree is fundamentally different, try to repeat the original example with its own parameters, I do not know what kind of events are in the names of this resource. Now I will correct part of the dispatch, in my example, the rest themselves .. - NewView