#1 2022-01-21 23:59:58

talexone
Member
Registered: 2013-08-21
Posts: 21

Websockets from MVCApplication

Is it possible to enbable websockets from MVCApplication?

Something like this:

aServer := TSQLRestServerDB.Create(aModel,ChangeFileExt(ExeVersion.ProgramFileName,'.db'));
aApplication := TBlogApplication.Create;
      try
        aApplication.Start(aServer);
        aServer.ServiceMethodRegisterPublishedMethods('', aApplication);
        aHTTPServer := TSQLHttpServer.Create('8092',aServer,'+',useBidirSocket);
        aHTTPServer.WebSocketsEnable(aServer,'').Settings.SetFullLog;

...

IChatService = interface(IServiceWithCallbackReleased)
    ['{D3108D0F-AC2A-4520-B3C3-A0F3695ECEBC}']
    procedure Join(const callback: IChatCallback);
    procedure Post(const msg: string);
  end; 
    
...

initialization
  TInterfaceFactory.RegisterInterfaces([
    TypeInfo(IChatService),TypeInfo(IChatCallback)]);

and then subscribe from javascript

  socket = new WebSocket("ws://localhost:8092/blog/chat/join","synopsejson");

Offline

#2 2022-01-22 07:38:38

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,238
Website

Re: Websockets from MVCApplication

Yes, but then you will have to emulate the  "synopsejson" protocol over the wire... smile

See TWebSocketProtocolJSON.FrameCompress and TWebSocketProtocolJSON.FrameDecompress for how the JSON content is encoded for incoming and outgoing frames, and how REST is emulated.
It is very powerful, but perhaps a bit complicated to implement from scratch. There is no JavaScript code available for it yet. sad

As an alternative, you could define your own WS protocol, the use it via JS WebSocket(). You can add some custom protocols to your main TWebSocketServerRest instance as created by the useBidirSocket mode.
You will have input/output JSON frames with no encapsulation.
See the WS chat sample.
I guess this is the easiest to do with a JS client. big_smile

Offline

#3 2022-01-22 23:12:48

talexone
Member
Registered: 2013-08-21
Posts: 21

Re: Websockets from MVCApplication

Thank you, the second option is what I need. I'll try that way.

Offline

Board footer

Powered by FluxBB