#1 2020-02-17 07:22:05

sakura
Member
From: Germany
Registered: 2018-02-21
Posts: 239
Website

WebSockets Client side

Hi,

using

  aWebSocketServer := THttpApiWebSocketServer.Create(False);
  aWebSocketServer.AddUrlWebSocket(aServerRoot, WebSocketsPort, ServerSecurity = secSSL, '*', True);
  aWebSocketServer.RegisterProtocol(aProtocolName, False, OnAccept, OnMessage, OnConnect, OnDisconnect);

and connecting to it from a browser client like

socket = new WebSocket(`wss://domain:8081/Test/`, 'protocol')
socket.onopen = _message => {
	console.log(_message)
	socket.send(JSON.stringify(['mediator', { register: true, service: 'xyz', channels: 'xyz', id:' blubb' }])
}
socket.onmessage = _message => { console.log(_message) }
socket.onerror = _message => { console.log(_message) }
socket.onclose = _message => { console.log(_message) }

works perfectly.

However, how could I connect from a Delphi client to the web sockets (used for push notifications), as I do from the browser? Which mORMot-class should I use. All samples I've found directly use interfaces, and similar.

Last edited by sakura (2020-02-17 07:23:19)

Offline

#2 2020-02-17 09:25:16

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

Re: WebSockets Client side

You can use the lower level WebSockets connection.

Something like:

  fWebSockets := THttpClientWebSockets.Create;
  if doWebSocketsLog in fOptions then
    WebSocketLog := TSynLog;
  fWebSockets.Open(fServerURI, '80'); // use port 80 until our client supports TLS
  if doWebSocketsLogVerbose in fOptions then
    fWebSockets.Settings.SetFullLog;
  proto := TWebSocketProtocolChat.Create(fWebSocketsProtocolName, '', OnFrameReceived);
  msg := fWebSockets.WebSocketsUpgrade(fWebSocketsUri, '', false, false, proto);
  if msg = '' then
    log.Log(sllTrace, 'ConnectToWebSockets: upgraded with protocol %', [fWebSocketsProtocolName], self)
  else
    raise ESynException.CreateUTF8('WebSocketsUpgrade: %', [msg]);

Offline

#3 2020-02-17 10:47:28

sakura
Member
From: Germany
Registered: 2018-02-21
Posts: 239
Website

Re: WebSockets Client side

Works, thanks. My problem was sending the frames, forgot to surround my string with StringToUTF8() in the test app and it never got sent. Works perfectly fine, now. big_smile

Offline

Board footer

Powered by FluxBB