Could anyone provide an example on parsing received JSON text?

Nov 7, 2011 at 4:35 AM

Dear All,

Could anyone provide an example on parsing received JSON text?

I can see in the definition of "OnNewMessageReceived" method that the received message has a type of string. I would like to know how to parse received JSON text (such as getting value, if the corresponding name is given) in the context of SuperWebSocket. Thanks very much!

Coordinator
Nov 7, 2011 at 5:00 AM

You'd better to follow the project JsonCommandAssembly in Samples directory of source code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperWebSocket.Samples.JsonCommandAssembly.JsonObject;
using SuperWebSocket.SubProtocol;

namespace SuperWebSocket.Samples.JsonCommandAssembly.Command
{
    /// <summary>
    /// When the client send "CHAT {'Sender':'kerry', 'Receiver': 'Linda', 'Content':'Where are you now?'}",
    /// the method of this class will be executed
    /// </summary>
    public class CHAT : JsonSubCommand<ChatMessage>
    {
        protected override void ExecuteJsonCommand(WebSocketSession session, ChatMessage commandInfo)
        {
            //Save to database or despacth to other session?
        }
    }
}

Nov 7, 2011 at 8:49 PM
Edited Nov 7, 2011 at 8:50 PM

Sorry, I am new to C#. I follow the instructions in "SuperWebSocket\Samples\JsonCommandAssembly\readme.txt". I mean, what I have done are:

1. Copy file "SuperWebSocket.Samples.JsonCommandAssembly.dll" and file "SuperWebSocket.Samples.JsonCommandAssembly.pdb" to directory "SuperWebSocket\SuperWebSocketService\bin\Release\"

2. Insert "commandAssembly="SuperWebSocket.Samples.JsonCommandAssembly" into file "SuperWebSocket\SuperWebSocketService\bin\Release\SuperWebSocket.Service.exe.config" and then rebuild the solution.

3. Insert "Console.WriteLine("inside CHAT.cs");" statement into the "ExecuteJsonCommand" method of the "CHAT" class.

4. Run the server. Use my JS code to send "CHAT {'Sender':'kerry', 'Receiver': 'Linda', 'Content':'Where are you now?'}" to the server via MozWebSocket.

But nothing happens. If the "ExecuteJsonCommand" method of the "CHAT" class is called, I should see "inside CHAT.cs" in the console, right? I am confused, could you point out what's gone wrong and provide me more information on how to parse JSON text?

Thank you!

 

 

Coordinator
Nov 8, 2011 at 1:16 AM

You should check logging files for more information.