Jump to content

Socket


picyka

Recommended Posts

Bom dia, quem está procurando uma solução para socket,

Eu recomendo

https://github.com/mateusvicente100/bird-socket-server

muito simples de usar Lembrando que o servidor de soquete deve ser separado do UNIGUI

Cliente:

 

class procedure TUniGUISocket.Connect(Main: TUniForm);
begin
  var ServidorURL := 'ws://' + Self.GetServidor;
  var SocketName := 'ws_' + Main.WebForm.JSName;

  var lScript :=
    '  try { ' +
    '     uniVars.' + SocketName + ' = new ReconnectingWebSocket("' + ServidorURL + '"); ' +
    '     uniVars.' + SocketName + '.addEventListener(''message'', function (event){ajaxRequest(' + Main.WebForm.JSName + ', "_socket", [''text='' + event.data]);}) ' +
    '  } ' +
    '  catch (e) { ' +
    '     console.log(''error socket open: '', e) ' +
    '  };';

  Main.UniSession.AddJS(lScript);
end;

class procedure TUniGUISocket.Disconnect(Main: TUniForm);
begin
  var ServidorURL := 'ws://' + Self.GetServidor;
  var SocketName := 'ws_' + Main.WebForm.JSName;
  var lScript :=
    '  try { ' +
    '     uniVars.' + SocketName + '.close() ' +
    '  } ' +
    '  catch (e) { ' +
    '     console.log(''error socket close: '', e) ' +
    '  };';

  Main.UniSession.AddJS(lScript);
end;

class procedure TUniGUISocket.ProcessMessage(Main: TUniForm; _Mensagem: String);
begin  
  if _Mensagem = _NEW_VERSION then
  begin
    if TMessageUtils.Question('A uma nova versão disponível, para evitar qualquer instabilidade é necessário atualizar.' + sLineBreak + '<b>Deseja atualizar agora?</b>', 'Atenção') = mrYes then
    begin
      UniSession.Logout;
      UniApplication.Restart;
    end;
  end
  else
  if _Mensagem = _NEW_AUTH then
    TMainForm(Main).UniTimerRecoonectSocket.Enabled := True
  else
  if _Mensagem = _LOGIN_SUCESS then
    TMainForm(Main).UniTimerRecoonectSocket.Enabled := False;
end;

class procedure TUniGUISocket.Send(Main: TUniForm; Value: String);
begin
  var ServidorURL := 'ws://' + Self.GetServidor;
  var SocketName := 'ws_' + Main.WebForm.JSName;
  var lScript :=
    '  try { ' +
    '     uniVars.' + SocketName + '.send(''' + Value + ''') ' +
    '  } ' +
    '  catch (e) { ' +
    '     console.log(''error socket send: '', e) ' +
    '  };';

  Main.UniSession.AddJS(lScript);
end;

Só para contribuir com a comunidade.

Sobre o cliente que eu estou usando

GitHub - joewalnes/reconconnecting-websocket: Um pequeno decorador para a API JavaScript WebSocket que se reconecta automaticamente

Reconectando o WebSocket espero que seja útil para alguém, Hugs.

 

  • Like 1
  • Upvote 1
Link to comment
Share on other sites

14 horas atrás, Wilton Ergon disse:

Estamos prestes a receber a versão do unigui com websockets nativos, qualquer solução externa, na minha opinião, é uma perda de tempo.

este projeto foi criado por um brasileiro da mesma equipe que o cavalo. para outros usos é muito bom e funciona bem

Good morning, I use 3 layers, so the socket server was out of unigui, by the tests I did it works. Note: It's still in testing, when it comes out native in unigui, I can change it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...