Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type
- TClient = class(TObject)
- public
- protocol : TWebSocketProtocol;
- serverResponse : THttpServerResp;
- end;
- TWebSocketProtocolEcho = class(TWebSocketProtocolChat)
- protected
- procedure EchoFrame(Sender: THttpServerResp; const Frame: TWebSocketFrame);
- public
- clients : TObjectList<TClient>;
- end;
- var
- Server: TWebSocketServer;
- protocol: TWebSocketProtocolEcho;
- procedure TWebSocketProtocolEcho.EchoFrame(Sender: THttpServerResp;
- const Frame: TWebSocketFrame);
- var
- client : TClient;
- begin
- TextColor(ccLightMagenta);
- write(GetEnumName(TypeInfo(TWebSocketFrameOpCode),ord(Frame.opcode))^,' - ');
- TextColor(ccWhite);
- case Frame.opcode of
- focContinuation:
- begin
- Self.clients.Add(tclient.Create);
- Self.clients.Last.protocol := Self;
- Self.clients.Last.serverResponse := Sender;
- write('Connected');
- end;
- focConnectionClose:
- write('Disconnected');
- focText,focBinary:
- begin
- for client in self.clients do
- begin
- write('Echoing ',length(Frame.payload),' bytes');
- SendFrame(client.serverResponse,Frame);
- end;
- end;
- end;
- TextColor(ccCyan);
- writeln(' from ',Sender.ServerSock.RemoteIP,'/',PtrInt(Sender.ServerSock.Sock));
- end;
- procedure Run;
- begin
- Server := TWebSocketServer.Create('8888',nil,nil,'test');
- try
- protocol := TWebSocketProtocolEcho.Create('meow','');
- protocol.clients := TObjectList<TClient>.Create(false);
- protocol.OnIncomingFrame := protocol.EchoFrame;
- Server.WebSocketProtocols.Add(protocol);
- TextColor(ccLightGreen);
- writeln('WebSockets Chat Server running on localhost:8888'#13#10);
- TextColor(ccWhite);
- writeln('Please load Project31SimpleEchoServer.html in your browser'#13#10);
- TextColor(ccLightGray);
- writeln('Press [Enter] to quit'#13#10);
- TextColor(ccCyan);
- readln;
- finally
- Server.Free;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement