推动器通道客户插件,用于针对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 >