JSBridge API

A JSBridge is provided in SDM mobile client’s DappBrowser, then H5 can all the Native functions.

Overview

JSBridge is simply speaking, mainly to provide JavaScript interfaces that call the Native function, so that the “front -end part” in mixed development can easily use Native functions such as address positions, cameras, and even payment. Since it is simply, the use of JSBridge must not only “call the Native function” as simple and broad. In fact, JSBridge is just like the meaning of “Bridge” in its name. It is a bridge between Native and non -Native. Its core is a channel for constructing message communication between Native and non -Native, and it is a channel for two -way communication.

js-bridge-intro

  1. H5 call Native:
    • Natively bind WebView JavaScript bridge on window , H5 directly calls the native receiving method in this object.
  2. Native call H5:
    • H5 binds JSBridge on window, and Native calls the H5 receiving method on this object through the native method.

Basic Flow

  1. DappBrowser will inject the global JS function SDMClient to the website, which declares the Feature APIS
  2. H5 would determine whether it is running in SDM DappBrowser by detecting whether there is SDMClient in the current window
    • When running in DappBrowser, H5 can calls feature api, eg: Sdmclient.${Feature_api}.(${params})
    • There may be a callback in ${params} which needs to follow the Callback specification
  3. DappBrowser will listen to all Feature API calls
    • Intercept Feature API call, execute Native subsequent logic
    • When calling back, native will call WebView.evalutejavaScript ()
      • Success: SDMClient.excuteCallbck(callbackID, null, result)
      • Failed: SdmClient.excuteCallback(callbackID, errorMessage)
    • The H5 side executes callback (if there is a callback in ${params})

Feature APIs

API1 Fetch AuthorizeCode

  • Description
    • Get some capabilities of SDM login users, avatars, nicknames, etc.
  • Call Method
    fetchAuthorizeCode(request_type, client_id, redirect_uri, cb)
    
  • Request
    • Required Params
      • request_type: <string> Request type (range: ‘code’)
      • client_id: <string> APP registered ID
      • redirect_uri: <string> APP registered redirect uri
    • Optional Params
  • Response
    {
        "authorization_code": "53ddd55b-c1c9-49b8-b623-xxx" 
    }
    
  • Example
     SdmClient.fetchAuthorizeCode('code','1111111111111', 'https://lucky.socialswap.com', cb)
    

API2 Show Squad Room List

  • Description

    • Close current browser, and open squad room list belongs to specified squad id
  • Call Method

    showSquadRoomList(squadId)
    
  • Request

    • Required Params
      • squadId<string> squad id
  • Response

    • NA
  • Example

    SdmClient.showSquadRoomList('111111')
    

API3 Share(App/System)

  • Description

    • Share url in App or operating system
  • Call Method

    forward(inApp, url)
    
  • Request

    • Required Params
      • inApp<int>
        • 1: share in app,
        • 0: share in operating system
      • url: <string> sharing url
  • Response

    • NA
  • Example

    SdmClient.forward(1, "www.google.com")
    

API4 Open Room

  • Description

    • Open a specified room under squad
  • Call Method

    openRoom(parentNode,childNode,squadId,roomName)
    
  • Request

    • Required Params
      • parentNode<string> main key
      • childNode: <string> child key
      • squadId: <string> squadId
      • roomName: <string> room name
  • Response

    • NA
  • Example

    SdmClient.openRoom(parentNode,childNode,squadId,roomName)
    

API5 Close Browser

  • Description

    • close current browser
  • Call Method

    closeBrowser()
    
  • Request

    • NA
  • Response

    • NA
  • Example

    SdmClient.closeBrowser()
    

API6 Fetch Room Context

  • Description

    • fetch context of the latest opened room
  • Call Method

    fetchRoomContext(cb)
    
  • Request

  • Response

    {
      "userId": "@xxx:hs.sending.me",                 //Current UserID (Required)           
      "roomId": "!qvovAGEYBhZlsDCoJG:hs.sending.me",  //RoomId (Required)
      "squadId": "!qvovAGEYBhZlsDCoJG:hs.sending.me", //SquadId (Optional)
      "isDirect": false                               // Direct Message Flag       (Required)      
    }
    
    • there is no context when returnnull
    • Room Type (DM/SquadRoom/Room)
      • isDirect whether DM(private/direct message)
      • squadId SquadRoom when it is not null
      • Room for the rest
  • Example

    SdmClient.fetchRoomContext(cb)
    

Callback Specification

  • Desciption
        //【Dapp】Callback
        // success:  cb(null, resultObj)
        // failed:   cb(error_msg, null)
        const cb:function(error, value) {}