友好社交云打字机 SDK
开始
- 以 npm 为单位 :
npm install @amityco/ts-sdk - 用纱线:
yarn add @amityco/ts-sdk
运行时间支持
- 现代浏览器( 符合 2017)
- 节点JS(通过esm进口)
- 反应- Native
创建客户端实例
要将您的用户连接到我们的平台,您需要创建客户端实例并将其连接.
import { Client } from '@amityco/ts-sdk';
(async () => {
const sessionHandler: Amity.SessionHandler = {
sessionWillRenewAccessToken(renewal: Amity.Renewal) {
renewal.renew();
/*
* If using an auth token
*
* try {
* renew.renewWithAuthToken(authToken)
* } catch() {
* sdk will try to renew again at a later time
*
* renew.unableToRetrieveAuthToken()
* }
*/
},
};
const client = Client.createClient(API_KEY, API_REGION);
let isConnected = await Client.login({ userId: USER_ID }, sessionHandler);
})();
这个 API_KEY 财务报告和审定财务报表 API_REGION 在您创建应用程序时给出 友好门户。 。 。 .
您可以使用 onClientDisconnected 函数 :
import { Client } from '@amityco/ts-sdk';
const unsub = Client.onClientDisconnected(() => (isConnected = false));
这个 unsub返回值为 处理函数 用于清理客户端应用程序的内存。 它被设计成与React完全吻合,在诸如:
useEffect(() => onClientConnected(() => setConnected(false)), [])
## Going further
### Domain types
All the necessary types are automatically exposed in the `Amity` namespace. As long as you import the SDK once, the namespace will be available in your workspace. You can start to type `Amity.` and see the list of types in your intellisense after having imported the SDK in any file of your project.
### Subscribing to real time events
We expose a couple of functions for your to receive simply the events that could fire from our servers, for example, if another user would change their display name, or if a channel would receive a new message.
The `subscribeTopic` method retuns an unsubscriber that can be called on clean up to stop recieving further events.
```ts
import { subscribeTopic, getUserTopic } from '@amityco/ts-sdk'
const unsub = subscribeTopic(getUserTopic({} as Amity.User))
使用 Live 对象观察对象
对于一个 "一无所有" 方法,我们暴露活的物体, 它允许获取物体 财务报告和审定财务报表 登记所有更改。 它的设计符合React的用法状态和使用效果:
import { UserRepository } from '@amityco/ts-sdk'
const Component = ({ userId }: { userId: string }) => {
const [user, setUser] = useState<Amity.User>({})
useEffect(() => UserRepository.getUser(userId, ({ data: user, loading, error }) => {
// ensure you call unsub() on cleanup or unmount
const unsub = subscribeTopic(getUserTopic(user))
setUser(user)
}), [userId])
return <div>{JSON.stringify(user, null, 2)}
}
使用 Live Collection 监视收藏
与活物概念类似,我们揭露活物收藏,可以取出活物收藏 财务报告和审定财务报表 登记所有更改。 它的设计符合React的用法状态和使用效果:
import { UserRepository.getUsers } from '@amityco/ts-sdk'
const Component = () => {
const [users, setUsers] = useState<Amity.User[]>({})
useEffect(() => UserRepository.getUsers({}, ({ data, loading, error, nextPage, hasNextPage }) => {
// ensure you call unsub() on cleanup or unmount
// Subscribe to each user in collection for getting real time updates
setUsers(users)
}), [userId])
return <div>{JSON.stringify(user, null, 2)}
}
文档
查阅综合文件请访问 我们的文档页面。 。 。 .
如果你有任何问题,你可以拜访我们 论坛。 。 。 .