| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714 |
- unit GroupClient;
- interface
- uses
- superobject, WebSocket2, WebSocketClient, Classes, Graphics, synautil,
- blcksock, SysUtils, DateUtils, GroupUtility, GroupConfig, GroupProtocols,
- RealICQModel;
- type
- TGroupClient = class
- private
- FClient: TTestWebSocketClientConnection;
- FProtocol: TGroupProtocol;
- FLoginName: string;
- FConnected: Boolean;
- FGroupMonitor: TObject;
- FLastRecvMsg: ISuperObject;
- procedure GetMyGroups;
- procedure OnOpen(aSender: TWebSocketCustomConnection);
- procedure OnRead(aSender: TWebSocketCustomConnection; aFinal, aRes1, aRes2, aRes3: boolean; aCode: integer; aData: TMemoryStream);
- procedure OnClose(aSender: TWebSocketCustomConnection; aCloseCode: integer; aCloseReason: string; aClosedByPeer: boolean);
- public
- constructor Create();
- destructor Destroy; override;
- procedure Send(AData: string); overload;
- procedure Send(AProtocol: string; AData: ISuperObject); overload;
- procedure Ping;
- function Connect(ALoginName: string = ''): Boolean;
- procedure Disconnect;
- { TODO -olqq -c : 群组操作 2015/6/2 9:50:42 }
- procedure CreateTeam(ATeamName, ATeamCallBoard, ATeamIntro: string; ATeamMembers: TStringList; AIsTempTeam: Boolean; ATeamValidateType: TRealICQTeamValidateType);
- procedure RemoveTeam(ATeamID: string);
- procedure UpdateTeamInfo(ATeamID, AIntro, ANotice, AName: string; AAuth: Integer);
- procedure UpdateTeamCreator(ATeamID, AIntro, ANotice, AName: string; AAuth: Integer; ACreator: string);
- procedure SearchTeam(ATeamID, AName, AIntro, ANotice: string; AMatchingType: TRealICQMatchingType; ASkip: Integer = 0; ALimit: Integer = 20);
- procedure AsynTeam(ATeamID: string);
- procedure GetHistoryMessageCount(ATeamID: string);
- procedure GetHistoryMessages(ATeamID: string; ts: Int64);
- procedure ChangeActiveDevice;
- { TODO -olqq -c : 群成员操作 2015/6/2 10:03:30 }
- procedure AddTeamMembers(ATeamID: string; AMembersID, AManagersID: TStringList);
- procedure RemoveTeamMembers(ATeamID: string; AMembersID, AManagersID: TStringList);
- procedure SetAlias(ATeamID, AMemberID, Alias: string);
- function GetAlias(ATeamID, AMemberID: string): string;
- procedure InviteJoin(ATeamID, AOperatorID, AOperatorMsg: string; AMembers: TStringList);
- procedure JoinTeam(ATeamID, AMsg: string);
- procedure JoinTeamResponse(ATeamID, ALoginName, ATag: string; Accepted: Boolean);
-
{ TODO -olqq -c : 群消息 2015/6/2 10:11:20 }
-
procedure SendTeamMessage(ATeamID, ASender, AMsg: string; AFont: TFont; AFaces: TStringList; Attachs: string);
- procedure GetOffineMessage;
- procedure MessageMisc(ATeamID: string);
- procedure MessageMiscMust(ATeamID: string);
- { TODO -olqq -c : 群图片 2015/6/3 9:39:26 }
-
function UploadFile(Hash, FileName, ID, GroupID: string): Boolean;
- procedure SendFilesRequest(AGroupId, AUserId, FileName: string);
- property Connected: Boolean read FConnected write FConnected;
- end;
- {$INCLUDE LXTalk.inc}
- implementation
- uses
- IdHTTP, LoggerImport, GroupMonitor, xFonts, IdMultipartFormData,
- HTTPApp, RealICQClient, RealICQUtility;
- const
- UPLOAD_URL: string = 'http://%s:%d/file/upload';
- { TGroupClient }
- procedure TGroupClient.CreateTeam(ATeamName, ATeamCallBoard, ATeamIntro: string; ATeamMembers: TStringList; AIsTempTeam: Boolean; ATeamValidateType: TRealICQTeamValidateType);
- var
- createGroup: string;
- jo, AData: ISuperObject;
- AMemebers: TSuperArray;
- iLoop: Integer;
- begin
- //jo := SO(GROUP_CREATE_JSON);
- jo := SO('{"id":"", "group":{"name":"", "type":"", "creator":"", "intro":"", "notice":"","auth":0,"members":[]}}');
- jo.S['id'] := '';
- jo.S['group.name'] := Trim(ATeamName);
- jo.I['group.type'] := 0;
- jo.S['group.creator'] := FLoginName;
- jo.S['group.intro'] := Trim(ATeamIntro);
- jo.S['group.notice'] := Trim(ATeamCallBoard);
- //修复新建群组验证类型无效BUG
- jo.I['group.auth'] := Integer(ATeamValidateType);
- AMemebers := jo.A['group.members'];
- for iLoop := 0 to ATeamMembers.Count - 1 do
- AMemebers.S[iloop] := ATeamMembers[iLoop];
- Send(GROUP_CREATE_REQUEST, jo);
- end;
- procedure TGroupClient.RemoveTeam(ATeamID: string);
- var
- jo: ISuperObject;
- begin
- jo := SO('{"id":"", "group":{"id":""}}');
- jo.S['group.id'] := ATeamID;
- Send(GROUP_DEL_REQUEST, jo);
- end;
- procedure TGroupClient.UpdateTeamInfo(ATeamID, AIntro, ANotice, AName: string; AAuth: Integer);
- var
- jo: ISuperObject;
- begin
- jo := SO('{"id":"", "group":{"id":"","intro":"","notice":"","name":"","auth":0}}');
- jo.S['group.id'] := ATeamID;
- jo.S['group.intro'] := AIntro;
- jo.S['group.notice'] := ANotice;
- jo.S['group.name'] := AName;
- jo.I['group.auth'] := AAuth;
- Send(GROUP_UPDATE_REQUEST, jo);
- end;
- procedure TGroupClient.UpdateTeamCreator(ATeamID, AIntro, ANotice, AName: string; AAuth: Integer; ACreator: string);
- var
- jo: ISuperObject;
- begin
- jo := SO('{"id":"", "group":{"id":"","intro":"","notice":"","name":"","auth":0}}');
- jo.S['group.id'] := ATeamID;
- jo.S['group.intro'] := AIntro;
- jo.S['group.notice'] := ANotice;
- jo.S['group.name'] := AName;
- jo.I['group.auth'] := AAuth;
- jo.S['group.creator'] := ACreator;
- Send(GROUP_UPDATE_REQUEST, jo);
- end;
- procedure TGroupClient.AsynTeam(ATeamID: string);
- var
- jo: ISuperObject;
- begin
- jo := SO('{"id":"","group":{"id":""}}');
- jo.S['group.id'] := ATeamID;
- Send(GROUP_GET_REQUEST, jo);
- end;
- procedure TGroupClient.SearchTeam(ATeamID, AName, AIntro, ANotice: string; AMatchingType: TRealICQMatchingType; ASkip: Integer = 0; ALimit: Integer = 20);
- var
- jo: ISuperObject;
- SearchGroup: string;
- begin
- jo := SO('{"id":"","group":{"id":"","intro":"","name":"","notice":""},"skip":0,"limit":0}');
- jo.S['group.id'] := ATeamID;
- if AIntro <> '' then
- begin
- if AMatchingType = mtLikeSearch then
- jo.S['group.intro'] := '*' + AIntro
- else
- jo.S['group.intro'] := AIntro;
- end;
- if AName <> '' then
- begin
- if AMatchingType = mtLikeSearch then
- jo.S['group.name'] := '*' + AName
- else
- jo.S['group.name'] := AName;
- end;
- if ANotice <> '' then
- begin
- if AMatchingType = mtLikeSearch then
- jo.S['group.notice'] := '*' + ANotice
- else
- jo.S['group.notice'] := ANotice;
- end;
- jo.I['skip'] := ASkip;
- jo.I['limit'] := ALimit;
- Send(GROUP_QUERY_REQUEST, jo);
- end;
- procedure TGroupClient.AddTeamMembers(ATeamID: string; AMembersID, AManagersID: TStringList);
- var
- jo: ISuperObject;
- ja, ja1: TSuperArray;
- iLoop: Integer;
- begin
- jo := SO('{"id":"", "group":{"id":"","members":[],"managers": []}}');
- jo.S['group.id'] := ATeamID;
- ja := jo.A['group.members'];
- if (AMembersID <> nil) then
- for iLoop := 0 to AMembersID.Count - 1 do
- ja.S[iloop] := AMembersID[iLoop];
- ja1 := jo.A['group.managers'];
- if (AManagersID <> nil) then
- for iLoop := 0 to AManagersID.Count - 1 do
- ja1.S[iloop] := AManagersID[iLoop];
- Send(MEMBER_ADD_REQUEST, jo);
- end;
- procedure TGroupClient.RemoveTeamMembers(ATeamID: string; AMembersID, AManagersID: TStringList);
- var
- jo: ISuperObject;
- ja, ja1: TSuperArray;
- iLoop: Integer;
- begin
- jo := SO('{"id":"", "group":{"id":"","members":[],"managers": []}}');
- jo.S['group.id'] := ATeamID;
- if AMembersID <> nil then
- begin
- ja := jo.A['group.members'];
- for iLoop := 0 to AMembersID.Count - 1 do
- ja.S[iloop] := AMembersID[iLoop];
- end;
- if AManagersID <> nil then
- begin
- ja1 := jo.A['group.managers'];
- for iLoop := 0 to AManagersID.Count - 1 do
- ja1.S[iloop] := AManagersID[iLoop];
- end;
- Send(MEMBER_DEL_REQUEST, jo);
- end;
- procedure TGroupClient.SetAlias(ATeamID, AMemberID, Alias: string);
- var
- jo, jo1: ISuperObject;
- ja: TSuperArray;
- begin
- jo := SO('{"id":"","group":{"id":"","members":[]}}');
- jo1 := SO('{"id":"","alias":""}');
- jo.S['group.id'] := ATeamID;
- jo1.S['id'] := AMemberID;
- jo1.S['alias'] := Alias;
- ja := jo.A['group.members'];
- ja.Add(jo1);
- Send(MEMBER_UPDATE_REQUEST, jo);
- end;
- procedure TGroupClient.InviteJoin(ATeamID, AOperatorID, AOperatorMsg: string; AMembers: TStringList);
- var
- jo: ISuperObject;
- ja: TSuperArray;
- ILoop: Integer;
- begin
- jo := SO('{"id":"","group":{"id":"","members":[], "operator":{"id":"","msg":""}}}');
- jo.S['group.id'] := ATeamID;
- jo.S['group.operator.id'] := AOperatorID;
- jo.S['group.operator.msg'] := AOperatorMsg;
- ja := jo.A['group.members'];
- for iLoop := 0 to AMembers.Count - 1 do
- ja.S[iloop] := AMembers[iLoop];
- Send(INVITE_REQUEST, jo);
- end;
- procedure TGroupClient.JoinTeam(ATeamID, AMsg: string);
- var
- jo: ISuperObject;
- begin
- jo := SO('{"group":{"id":"","apply":{"uid":"","msg":""}}}');
- jo.S['group.id'] := ATeamID;
- jo.S['group.apply.msg'] := AMsg;
- jo.S['group.apply.uid'] := FLoginName;
- Send(MEMBER_APPLY_REQUEST, jo);
- end;
- procedure TGroupClient.JoinTeamResponse(ATeamID, ALoginName, ATag: string; Accepted: Boolean);
- var
- jo: ISuperObject;
- begin
- jo := SO('{"id":"","group":{"id":"","apply":{"uid":""},"reply":{"uid":"", "ret":0}}}');
- jo.S['group.id'] := ATeamID;
- jo.S['group.apply.uid'] := ALoginName;
- jo.S['group.reply.uid'] := FLoginName;
- jo.S['group.reply.msg'] := ATag;
- if Accepted then
- jo.I['group.reply.ret'] := 1
- else
- jo.I['group.reply.ret'] := 0;
- Send(MEMBER_REPLY_REQUEST, jo);
- end;
- procedure TGroupClient.SendTeamMessage(ATeamID, ASender, AMsg: string; AFont: TFont; AFaces: TStringList; Attachs: string);
- var
- jo, jofile, joNotify, joFont: ISuperObject;
- ja, jaHashs: TSuperArray;
- iLoop: Integer;
- sendstr: string;
- AFace: TFace;
- IMG_TAG, ATT_TAG: string;
- begin
- if Attachs <> '' then
- ATT_TAG := ',"attach":[]';
- if AFaces.Count > 0 then
- IMG_TAG := ',"img":[]';
- jo := SO('{"group":{"id":""},"content":""}');
- // if (Attachs = '') and (AFaces.Count = 0) then
- // jo := SO('{"group":{"id":""},"txt":""}')
- // else if (Attachs = '') and (AFaces.Count > 0) then
- // jo := SO('{"grou p":{"id":""},"txt":"","img":[]}')
- // else if (Attachs <> '') and (AFaces.Count = 0) then
- // jo := SO('{"group":{"id":""},"txt":"","attach":[]}')
- // else
- // jo := SO('{"group":{"id":""},"txt":"","img":[],"attach":[]}');
- jo.S['group.id'] := ATeamID;
- jo.S['content'] := AMsg;
- jo.I['isDes'] := 0;
- jo.I['msgType'] := 1;
- jo.I['fromClient'] := 0;
- jo.I['networkType'] := 0;
- joFont := FontToJson(AFont);
- if joFont <> nil then
- jo.O['style'] := joFont;
- if Attachs <> '' then
- begin
- jo.A['attach'].S[0] := Attachs;
- end;
- if AFaces.Count > 0 then
- begin
- joNotify := SO('{"group":{"id":""},"hashs":[]}');
- joNotify.S['group.id'] := ATeamID;
- jaHashs := joNotify.A['hashs'];
- for iLoop := 0 to AFaces.Count - 1 do
- begin
- AFace := AFaces.Objects[iLoop] as TFace;
- jofile := SO();
- jofile.S['file'] := ExtractFileName(AFace.FileName);
- jofile.S['hash'] := LowerCase(AFace.MD5Code);
- // jo.A['img'].Add(jofile);
- if UploadFile(jofile.S['hash'], jofile.S['file'], '', ATeamID) then
- jaHashs.Add(jofile);
- end;
- Send(MESSAGE_SAY_REQUEST, jo);
- if jaHashs.Length > 0 then
- Send(FILE_UPLOADED_REQUEST, joNotify);
- Exit;
- end;
- Send(MESSAGE_SAY_REQUEST, jo);
- {$IFDEF OldGroup}
- MessageMisc(ATeamID);
- {$ELSE}
- ChangeActiveDevice;
- MessageMisc(ATeamID);
- {$ENDIF}
-
if Assigned(AFaces) and (AFaces.Count > 0) then
-
TRealICQUtility.FreeStringList(AFaces as TStringList);
-
end;
- procedure TGroupClient.MessageMisc(ATeamID: string);
- var
- ATick: Cardinal;
- begin
- ATick := FLastRecvMsg.I[ATeamID];
- if (ATick <> 0) and (GetTick - ATick < 5000) then
- Exit;
- MessageMiscMust(ATeamID);
- FLastRecvMsg.I[ATeamID] := GetTick;
- end;
- procedure TGroupClient.MessageMiscMust(ATeamID: string);
- var
- jo: ISuperObject;
- begin
- jo := SO('{"group":[]}');
- jo.A['group'].S[0] := ATeamID;
- Send(MARK_MK_REQUEST, jo);
- end;
- function GetSID(AResponseStr: string): string;
- var
- ACode: Byte;
- AStream: TStringStream;
- Len: Int64;
- begin
- Result := '';
- AStream := TStringStream.Create(AResponseStr);
- try
- AStream.Position := 1;
- Len := 0;
- AStream.Read(ACode, 1);
- while ACode <> $FF do
- begin
- Len := Len * 10;
- Inc(Len, ACode);
- AStream.Read(ACode, 1);
- end;
- //Code
- AStream.Read(ACode, 1);
- Result := AStream.ReadString(AStream.Size - AStream.Position);
- finally
- FreeAndNil(AStream);
- end;
- end;
- //{"socket":{"id":"639d9b0204f343a2acd5dfc3bf38b7f0","type":"mix","port":6714,"ver":"3.2.0","ip":"192.168.1.43","onlines":0},"storage":{"host":"127.0.0.1","port":6713}}
- function GatewayResponse(AJsonStr: string): Boolean;
- var
- jo: ISuperObject;
- config: TGroupConfig;
- begin
- Result := False;
- jo := SO(AJsonStr);
- if jo = nil then
- Exit;
- config := TGroupConfig.GetConfig;
- config.IP := jo.S['socket.host'];
- config.Port := jo.I['socket.port'];
- config.ImageIP := jo.S['storage.host'];
- config.ImagePort := jo.I['storage.port'];
- Result := True;
- end;
- procedure TGroupClient.ChangeActiveDevice;
- var
- jo: ISuperObject;
- begin
- jo := SO();
- jo.S['deviceID'] := 'pc';
- jo.B['on'] := True;
- Send(MARK_A_Q, jo);
- end;
- function TGroupClient.Connect(ALoginName: string = ''): Boolean;
- var
- AIdHttp: TIdHTTP;
- ResponeStr, sid: string;
- config: TGroupConfig;
- AURL: string;
- begin
- if ALoginName <> '' then
- FLoginName := ALoginName;
- if Trim(FLoginName) = '' then
- Exit;
- AIdHttp := TIdHTTP.Create(nil);
- try
- config := TGroupConfig.GetConfig;
- if config.GatewayEnable then
- begin
- config.RandomGatewayServer;
- AURL := Format(GATEWAY_URL, [config.GatewayIP, config.GatewayPort]);
- ResponeStr := AIdHttp.Get(AURL);
- if not GatewayResponse(ResponeStr) then
- Exit;
- end;
- SUCCESS(Format('%s:%d', [config.IP, config.Port]), '群组服务地址');
- AURL := Format(SHAKEHANDS_STEP1, [config.IP, config.Port, HTTPEncode(AnsiToUtf8(FLoginName)), (DateTimeToUnix(Now) - 8 * 60 * 60) * 1000]);
- ResponeStr := AIdHttp.get(AURL);
- ResponeStr := GetSID(ResponeStr);
- sid := SO(ResponeStr).S['sid'];
- AURL := Format(SHAKEHANDS_STEP2, [config.IP, config.Port, HTTPEncode(AnsiToUtf8(FLoginName)), (DateTimeToUnix(Now) - 8 * 60 * 60) * 1000, sid]);
- ResponeStr := AIdHttp.get(AURL);
- if FClient <> nil then
- FreeAndNil(FClient);
- AURL := Format(WEBSOCKET_URL, [HTTPEncode(AnsiToUtf8(FLoginName)), sid]);
- FClient := TTestWebSocketClientConnection.Create(config.IP, IntToStr(config.Port), AURL, '-', 'ws');
- FClient.OnRead := OnRead;
- FClient.OnClose := OnClose;
- FClient.OnOpen := OnOpen;
- FClient.Start;
- except
- on E: Exception do
- begin
- Freeandnil(AIdHttp);
- Error(E.Message, 'TGroupClient.Connect');
- Exit;
- end;
- end;
- Freeandnil(AIdHttp);
- end;
- function TGroupClient.UploadFile(Hash, FileName, ID, GroupID: string): Boolean;
- var
- AHttp: TIdHttp;
- MutPartForm: TIdMultiPartFormDataStream;
- Ret: TStringStream;
- response, UpUrl: string;
- jo, t, jofile: ISuperObject;
- SendStr: string;
- config: TGroupConfig;
- begin
- Result := False;
- config := TGroupConfig.GetConfig;
- UpUrl := Format(UPLOAD_URL, [config.ImageIP, config.ImagePort]);
- AHttp := Tidhttp.Create(nil);
- AHttp.Request.ContentType := 'multipart/form-data';
- AHttp.HandleRedirects := true;
- AHttp.AllowCookies := true;
- MutPartForm := TIdMultiPartFormDataStream.Create;
- MutPartForm.AddFile('file1', TRealICQClient.GetReceivedFaceDir + FileName, '');
- try
- response := AHttp.Post(UpUrl, MutPartForm);
- finally
- MutPartForm.Free;
- AHttp.Free;
- end;
- if response = 'ok' then
- Result := True;
- end;
- constructor TGroupClient.Create;
- begin
- FGroupMonitor := TGroupMonitor.Create;
- FProtocol := TGroupProtocol.Create(Self);
- FLastRecvMsg := SO('{}');
- end;
- destructor TGroupClient.Destroy;
- var
- PInt: PInteger;
- begin
- FreeAndNil(FGroupMonitor);
- if FClient <> nil then
- FreeAndNil(FClient);
- FLastRecvMsg := nil;
- FProtocol.Free;
- inherited;
- end;
- procedure TGroupClient.Disconnect;
- begin
- (FGroupMonitor as TGroupMonitor).Stop;
- (FGroupMonitor as TGroupMonitor).KeepAlive := False;
- if FConnected then
- begin
- FClient.Close(wsCloseNormal, 'goodbye');
- FConnected := False;
- end;
- end;
- function TGroupClient.GetAlias(ATeamID, AMemberID: string): string;
- begin
- end;
- procedure TGroupClient.GetHistoryMessageCount(ATeamID: string);
- var
- jo: ISuperObject;
- begin
- jo := SO();
- jo.N['filter'].S['group'] := ATeamID;
- Send(HISTORY_C_Q, jo);
- end;
- {
- group: <id>|<Group>, // 群组编号
- ts: <Number>|<String>, // * > >= < <= = Number
- skip: <Number>, // *偏移
- limit: <Number> // *限制
- }
- procedure TGroupClient.GetHistoryMessages(ATeamID: string; ts: Int64);
- var
- jo, joFilter: ISuperObject;
- begin
- {$IFNDEF OldGroup}
- if ts = 0 then
- Exit;
- jo := SO();
- joFilter := SO();
- joFilter.S['group'] := ATeamID;
- joFilter.S['ts'] := '>' + IntToStr(ts);
- joFilter.I['limit'] := 10000;
- jo.O['filter'] := joFilter;
- Send(HISTORY_H_Q, jo);
- {$ENDIF}
- end;
- procedure TGroupClient.GetMyGroups;
- var
- jo: ISuperObject;
- begin
- jo := SO('{}');
- jo.S['user'] := FLoginName;
- Send(USER_LI_REQUEST, jo);
- end;
- procedure TGroupClient.GetOffineMessage;
- var
- jo: ISuperObject;
- begin
- // jo := SO('{}');
- // jo.S['uid'] := FLoginName;
- // Send(MESSAGE_OFFLINE_REQUEST, jo);
- end;
- procedure TGroupClient.OnClose(aSender: TWebSocketCustomConnection; aCloseCode: integer; aCloseReason: string; aClosedByPeer: boolean);
- begin
- FConnected := False;
- FClient := nil;
- (FGroupMonitor as TGroupMonitor).Stop;
- if (aClosedByPeer) or (aCloseCode <> 1000) then
- begin
- Error(Format('异常断开. Code:%d;Reson:%s;ClolsedByPeer:%s;', [aCloseCode, aCloseReason, BoolToStr(aClosedByPeer)]), 'TGroupClient.OnClose');
- end
- else
- Success(Format('正常断开. Code:%d;Reson:%s;ClolsedByPeer:%s;', [aCloseCode, aCloseReason, BoolToStr(aClosedByPeer)]), 'TGroupClient.OnClose');
- end;
- procedure TGroupClient.OnOpen(aSender: TWebSocketCustomConnection);
- begin
- FConnected := True;
- FClient.SendText('2probe');
- (FGroupMonitor as TGroupMonitor).Start(Self);
- (FGroupMonitor as TGroupMonitor).KeepAlive := True;
- end;
- procedure TGroupClient.OnRead(aSender: TWebSocketCustomConnection; aFinal, aRes1, aRes2, aRes3: boolean; aCode: integer; aData: TMemoryStream);
- var
- s: UTF8String;
- Recvdata: string;
- c: TTestWebSocketClientConnection;
- begin
- try
- (FGroupMonitor as TGroupMonitor).ReflashLastTime;
- c := TTestWebSocketClientConnection(aSender);
- s := ReadStrFromStream(c.ReadStream, c.ReadStream.size);
- if (c.ReadCode = wsCodeText) then
- Recvdata := utf8toansi(s)//CharsetConversion(s, UTF_8, GetCurCP)
- else
- Recvdata := s;
- if SameText(Recvdata, '3probe') then
- begin
- FClient.SendText('5');
- GetMyGroups;
- Exit;
- end;
- if (Length(Recvdata) < 4) or (FProtocol = nil) then
- Exit;
- Success(Recvdata, 'TGroupClient.OnRead');
- FProtocol.Proccess(Recvdata);
- except
- on E: Exception do
- Error(E.Message, 'TGroupClient.OnRead(' + Recvdata + ')');
- end;
- end;
- procedure TGroupClient.Ping;
- begin
- if (FClient <> nil) and (not FClient.Closed) then
- FClient.SendText('2');
- end;
- procedure TGroupClient.Send(AProtocol: string; AData: ISuperObject);
- var
- AJo: ISuperObject;
- begin
- AJo := SO('[]');
- AJo.AsArray.S[0] := AProtocol;
- AJo.AsArray.O[1] := AData;
- Send('42' + AJo.Asjson);
- end;
- procedure TGroupClient.Send(AData: string);
- begin
- try
- if (FClient <> nil) and (not FClient.Closed) then
- FClient.SendText(AnsiToUTF8(AData));
- Success(AData, 'TGroupClient.Send');
- except
- on E: Exception do
- begin
- Error(E.Message, 'TGroupClient.Send(' + AData + ')');
- end;
- end;
- end;
- procedure TGroupClient.SendFilesRequest(AGroupId, AUserId, FileName: string);
- begin
- end;
- end.
|