推動器通道客戶插件,用於針對Android和iOS的顫動。它包裝了Pusher-websocket-Java v2.2.5和Pusher-websocket-Swift v8.0.0。
有關推車渠道的教程和更多深入信息,請訪問官方文檔。
該客戶端與官方推動器服務器和Laravel自託管Pusher Websocket Server(Laravel-Websockets)一起使用。
添加到您的pubspec.yaml
dependencies :
pusher_client : ^2.0.0 將Podfile中的最小部署目標設置為9.0。轉到ios/Podfile ,然後刪除這一行:
# platform :ios, '8.0'
將其更改為:
platform :ios, '9.0'
如果您使用的是Laravel-websockets等本地推動器服務器,則可能會訂閱專用頻道,請訪問ios/Runner/Info.plist並添加:
< key >NSAppTransportSecurity</ key >
< dict >
< key >NSAllowsArbitraryLoads</ key >
< true />
</ dict >如果您知道將連接到哪個域,請添加:
< key >NSAppTransportSecurity</ key >
< dict >
< key >NSExceptionDomains</ key >
< dict >
< key >example.com</ key >
< dict >
< key >NSExceptionAllowsInsecureHTTPLoads</ key >
< true />
< key >NSIncludesSubdomains</ key >
< true />
</ dict >
</ dict >
</ dict >如果您已使用R8或Proguard啟用了Code Obfuscation,則需要在android/app/build.gradle中添加以下規則:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile( ' proguard-android.txt ' ), ' proguard-rules.pro '
}
}然後在android/app/proguard-rules.pro中:
- keep class com.github.chinloyal.pusher_client .** { * ; } 簡而言之,這是API。
PusherOptions options = PusherOptions (
host : 'example.com' ,
wsPort : 6001 ,
encrypted : false ,
auth : PusherAuth (
'http://example.com/auth' ,
headers : {
'Authorization' : 'Bearer $ token ' ,
},
),
);
PusherClient pusher = PusherClient (
YOUR_APP_KEY ,
options,
autoConnect : false
);
// connect at a later time than at instantiation.
pusher. connect ();
pusher. onConnectionStateChange ((state) {
print ( "previousState: ${ state . previousState }, currentState: ${ state . currentState }" );
});
pusher. onConnectionError ((error) {
print ( "error: ${ error . message }" );
});
// Subscribe to a private channel
Channel channel = pusher. subscribe ( "private-orders" );
// Bind to listen for events called "order-status-updated" sent to "private-orders" channel
channel. bind ( "order-status-updated" , ( PusherEvent event) {
print (event.data);
});
// Unsubscribe from channel
pusher. unsubscribe ( "private-orders" );
// Disconnect from pusher service
pusher. disconnect ();
有關參考格式的更多信息,請參見下面。
該構造函數採用一個應用程序密鑰,您可以從應用程序儀表板中的應用程序的API訪問部分和Pusher選項對像中獲取該密鑰。
PusherClient pusher = PusherClient ( YOUR_APP_KEY , PusherOptions ());如果您要使用私有,在線或加密的頻道,則需要在身份驗證訂閱時提供供應PusherAuth 。為了做到這一點,您需要傳遞具有auth集的PusherOptions對象。
PusherAuth auth = PusherAuth (
// for auth endpoint use full url
'http://example.com/auth' ,
headers : {
'Authorization' : 'Bearer $ token ' ,
},
);
PusherOptions options = PusherOptions (
auth : auth
);
PusherClient pusher = PusherClient ( YOUR_APP_KEY , options);禁用日誌記錄和自動連接執行此操作:
PusherClient pusher = PusherClient (
YOUR_APP_KEY ,
options,
enableLogging : false ,
autoConnect : false ,
);如果禁用了自動連接,則可以在按鈕實例上使用connect()手動連接。
該插件的大多數功能都是通過PusherOptions對象配置的。您可以通過在對像上設置參數來配置它,然後再將其傳遞給Pusher客戶端。下面是包含您可以設置的所有屬性的表。
| 方法 | 範圍 | 描述 |
|---|---|---|
| 加密 | 布爾 | 是否應使用TLS建立連接。 |
| auth | Pusherauth | 在對私人,私人加密和在線渠道進行身份驗證時,設置要使用的授權選項。 |
| 主持人 | 細繩 | 將與之建立連接的主機。 |
| WSPORT | int | 將建立未加密連接的端口。自動設置正確。 |
| WSSPORT | int | 將建立加密連接的端口。自動設置正確。 |
| 簇 | 細繩 | 設置客戶端將連接到的群集,從而正確設置主機和端口。 |
| ActivityTimeout | int | 將觸發“ ping”以檢查連接的無活動性的毫秒數。默認值為120,000。 |
| Pongtimeout | int | 客戶端等待在斷開連接之前收到服務器的“乒乓球”響應的毫秒數。默認值為30,000。 |
| MaxReconnectionAttement | int | 調用pusher.connect()時將進行重新連接嘗試的數量,之後客戶將放棄。 |
| maxReconnectgapinseconds | int | 兩個重新連接的延遲呈指數擴展(1、2、4,..秒)此屬性設置了兩個重新連接嘗試之間的最大值。 |
如果連接丟失,例如設備失去接收,則connect()方法還用於重新連接。請注意,一旦連接重新建立連接,在斷開連接並重新連接時將保留通道訂閱和事件綁定的狀態。
pusher. disconnect ();斷開連接後, PusherClient實例將發布任何內部分配的資源(線程和網絡連接)
通道使用渠道的概念作為訂閱數據的一種方式。它們是通過簡單的名稱來識別並訂閱的。事件被綁定到頻道,也可以通過名稱確定。
如上所述,通道訂閱只需要通過PusherClient實例一次註冊一次。它們在斷開連接之間保存,並與服務器重新連接重新建立。他們不應重新註冊。但是,在第一個connect之前,他們可能會在PusherClient實例上註冊它們 - 一旦連接可用,它們將在服務器上完成。
訂閱通道的默認方法涉及調用客戶端對象的訂閱方法:
Channel channel = pusher. subscribe ( "my-channel" );這返回一個Channel對象,該對象可以綁定到哪些事件。
私人渠道的創建方式與公共渠道完全相同,除了它們位於“私人”名稱空間中。這意味著將頻道名稱前綴:
Channel privateChannel = pusher. subscribe ( "private-status-update" );訂閱私人渠道涉及客戶認證。有關更多信息,請參閱“推送器構造函數”部分。
與私人頻道類似,您也可以訂閱私人加密頻道。該插件完全支持端到端加密。這意味著只有您和您的連接客戶才能閱讀您的消息。推動者無法解密它們。這些渠道必須以“私人加密 - ”的前綴為前綴。
與私人頻道一樣,您必須提供身份驗證端點。該端點必須使用支持端到端加密的服務器客戶端。有一個演示端點可以使用nodejs。
存在通道是其名稱由“存在 - ”前綴的通道。存在渠道也需要進行身份驗證。
Channel presenceChannel = pusher. subscribe ( "presence-another-channel" );通道訂閱上發生了兩種類型的事件。
Channel channel = pusher. subscribe ( "private-orders" );
channel. bind ( "order-status-updated" , ( PusherEvent event) {
print (event.data);
});您綁定的回調會收到PusherEvent :
| 財產 | 類型 | 描述 |
|---|---|---|
eventName | String | 事件的名稱。 |
channelName | String | 事件觸發的頻道的名稱。 (選修的) |
data | String | 傳遞給trigger的數據,編碼為字符串。如果您通過一個對象,則將序列化與JSON字符串,您可以根據需要解析。 (選修的) |
userId | String | 觸發事件的用戶的ID。這僅適用於在存在頻道上觸發的客戶事件。 (選修的) |
您可以通過以下方式與事件無關:
channel. unbind ( "order-status-updated" );一旦授權了私人或在場訂閱並獲得訂閱成功,就可以在這些渠道上觸發事件。
客戶觸發的事件稱為客戶端事件。因為它們是從客戶端觸發的,而這些客戶可能不信任,因此使用時有許多強制性規則。其中一些規則包括:
channel. bind ( "pusher:subscription_succeeded" , ( PusherEvent event) {
channel. trigger ( "client-istyping" , { "name" : "Bob" });
});有關完整詳細信息,請參見客戶端事件文檔。
連接後,您可以訪問當前客戶端連接的唯一標識符。這被稱為套接字ID 。一旦建立連接如下:您可以訪問該值:
String socketId = pusher. getSocketId ();有關如何以及為什麼有套接字ID的更多信息,請參閱有關身份驗證用戶和不包括收件人的文檔。
iOS日誌記錄似乎不會輸出彈奏控制台,但是,如果您從Xcode運行該應用程序,則應該可以看到日誌。
如果使用本地推動器服務器但無法訂閱專用頻道,請將其添加到您的iOS/Runner/info.plist:
< key >NSAppTransportSecurity</ key >
< dict >
< key >NSAllowsArbitraryLoads</ key >
< true />
</ dict >如果您知道將連接到哪個域,請添加:
< key >NSAppTransportSecurity</ key >
< dict >
< key >NSExceptionDomains</ key >
< dict >
< key >example.com</ key >
< dict >
< key >NSExceptionAllowsInsecureHTTPLoads</ key >
< true />
< key >NSIncludesSubdomains</ key >
< true />
</ dict >
</ dict >
</ dict >