Connections.js 5.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. export class MainConnection{constructor(){this._onMessage=null;this._onDisconnect=null;this._messageBuffer='';this._messageSize=0;this._eventListeners=[Host.InspectorFrontendHost.events.addEventListener(Host.InspectorFrontendHostAPI.Events.DispatchMessage,this._dispatchMessage,this),Host.InspectorFrontendHost.events.addEventListener(Host.InspectorFrontendHostAPI.Events.DispatchMessageChunk,this._dispatchMessageChunk,this),];}
  2. setOnMessage(onMessage){this._onMessage=onMessage;}
  3. setOnDisconnect(onDisconnect){this._onDisconnect=onDisconnect;}
  4. sendRawMessage(message){if(this._onMessage){Host.InspectorFrontendHost.sendMessageToBackend(message);}}
  5. _dispatchMessage(event){if(this._onMessage){this._onMessage.call(null,(event.data));}}
  6. _dispatchMessageChunk(event){const messageChunk=(event.data['messageChunk']);const messageSize=(event.data['messageSize']);if(messageSize){this._messageBuffer='';this._messageSize=messageSize;}
  7. this._messageBuffer+=messageChunk;if(this._messageBuffer.length===this._messageSize){this._onMessage.call(null,this._messageBuffer);this._messageBuffer='';this._messageSize=0;}}
  8. disconnect(){const onDisconnect=this._onDisconnect;Common.EventTarget.removeEventListeners(this._eventListeners);this._onDisconnect=null;this._onMessage=null;if(onDisconnect){onDisconnect.call(null,'force disconnect');}
  9. return Promise.resolve();}}
  10. export class WebSocketConnection{constructor(url,onWebSocketDisconnect){this._socket=new WebSocket(url);this._socket.onerror=this._onError.bind(this);this._socket.onopen=this._onOpen.bind(this);this._socket.onmessage=messageEvent=>{if(this._onMessage){this._onMessage.call(null,(messageEvent.data));}};this._socket.onclose=this._onClose.bind(this);this._onMessage=null;this._onDisconnect=null;this._onWebSocketDisconnect=onWebSocketDisconnect;this._connected=false;this._messages=[];}
  11. setOnMessage(onMessage){this._onMessage=onMessage;}
  12. setOnDisconnect(onDisconnect){this._onDisconnect=onDisconnect;}
  13. _onError(){this._onWebSocketDisconnect.call(null);this._onDisconnect.call(null,'connection failed');this._close();}
  14. _onOpen(){this._socket.onerror=console.error;this._connected=true;for(const message of this._messages){this._socket.send(message);}
  15. this._messages=[];}
  16. _onClose(){this._onWebSocketDisconnect.call(null);this._onDisconnect.call(null,'websocket closed');this._close();}
  17. _close(callback){this._socket.onerror=null;this._socket.onopen=null;this._socket.onclose=callback||null;this._socket.onmessage=null;this._socket.close();this._socket=null;this._onWebSocketDisconnect=null;}
  18. sendRawMessage(message){if(this._connected){this._socket.send(message);}else{this._messages.push(message);}}
  19. disconnect(){let fulfill;const promise=new Promise(f=>fulfill=f);this._close(()=>{if(this._onDisconnect){this._onDisconnect.call(null,'force disconnect');}
  20. fulfill();});return promise;}}
  21. export class StubConnection{constructor(){this._onMessage=null;this._onDisconnect=null;}
  22. setOnMessage(onMessage){this._onMessage=onMessage;}
  23. setOnDisconnect(onDisconnect){this._onDisconnect=onDisconnect;}
  24. sendRawMessage(message){setTimeout(this._respondWithError.bind(this,message),0);}
  25. _respondWithError(message){const messageObject=JSON.parse(message);const error={message:'This is a stub connection, can\'t dispatch message.',code:Protocol.DevToolsStubErrorCode,data:messageObject};if(this._onMessage){this._onMessage.call(null,{id:messageObject.id,error:error});}}
  26. disconnect(){if(this._onDisconnect){this._onDisconnect.call(null,'force disconnect');}
  27. this._onDisconnect=null;this._onMessage=null;return Promise.resolve();}}
  28. export class ParallelConnection{constructor(connection,sessionId){this._connection=connection;this._sessionId=sessionId;this._onMessage=null;this._onDisconnect=null;}
  29. setOnMessage(onMessage){this._onMessage=onMessage;}
  30. setOnDisconnect(onDisconnect){this._onDisconnect=onDisconnect;}
  31. sendRawMessage(message){const messageObject=JSON.parse(message);if(!messageObject.sessionId){messageObject.sessionId=this._sessionId;}
  32. this._connection.sendRawMessage(JSON.stringify(messageObject));}
  33. disconnect(){if(this._onDisconnect){this._onDisconnect.call(null,'force disconnect');}
  34. this._onDisconnect=null;this._onMessage=null;return Promise.resolve();}}
  35. export async function initMainConnection(createMainTarget,websocketConnectionLost){Protocol.Connection.setFactory(_createMainConnection.bind(null,websocketConnectionLost));await createMainTarget();Host.InspectorFrontendHost.connectionReady();Host.InspectorFrontendHost.events.addEventListener(Host.InspectorFrontendHostAPI.Events.ReattachMainTarget,()=>{SDK.targetManager.mainTarget().router().connection().disconnect();createMainTarget();});return Promise.resolve();}
  36. export function _createMainConnection(websocketConnectionLost){const wsParam=Root.Runtime.queryParam('ws');const wssParam=Root.Runtime.queryParam('wss');if(wsParam||wssParam){const ws=wsParam?`ws://${wsParam}`:`wss://${wssParam}`;return new WebSocketConnection(ws,websocketConnectionLost);}else if(Host.InspectorFrontendHost.isHostedMode()){return new StubConnection();}
  37. return new MainConnection();}
  38. self.SDK=self.SDK||{};SDK=SDK||{};SDK.MainConnection=MainConnection;SDK.WebSocketConnection=WebSocketConnection;SDK.StubConnection=StubConnection;SDK.ParallelConnection=ParallelConnection;SDK.initMainConnection=initMainConnection;SDK._createMainConnection=_createMainConnection;