stream chat net
2.6.0
官方.NET API客户端用于流聊天,这是用于构建聊天应用程序的服务。
探索文档»
代码样本·报告错误·请求功能
您可以在我们的“启动”页面上注册流帐户。
您可以使用此库访问聊天API端点服务器端。
对于客户端集成(Web和移动),请查看JavaScript,iOS和Android SDK库(DOCS)。
破坏v1.0 <的变化
该图书馆在V1.0中收到了许多更改,以使其更易于使用,并且将来更加维护。主要的更改是,
Channel和Client类都已经分为我们称为客户的小模块。 (这也重新销售了我们的Java库的结构。)主要更改:
Channel和Client端类已消失,并已在StreamChat.Clients名称空间中组织成较小的客户端。- 这些客户端无法保持状态,因为
Channel用来较早地将其保留在存储器中的channelType和channelId。因此,这意味着您需要将channelType和channelId传递到IChannelClient中的许多方法调用。- 异步方法名称现在具有
Async后缀。- 所有公共方法和课程都有文档。
- 标识符已重命名为
ID到Id,以遵循Microsoft的命名指南。例如userIDID->userId。- 许多数据类都被重命名为更有意义。例如
ChannelObject- >Channel。- 数据类已移至
StreamChat.Models名称空间。- 全功能奇偶校验:所有后端API都可以使用。
- 返回的值是使用
GetRateLimit()方法的ApiResponse的类型,并揭示了速率限制Informaiton。- 该项目的文件夹结构已重组,以遵循微软的建议。
- 单位测试得到了改进。它们更小,更专注,并具有清理方法。
- 添加了.NET 6.0支持。
图书馆的适当用法:
var clientFactory = new StreamClientFactory ( "YourApiKey" , "YourApiSecret" ) ; // Note: all client instances can be used as a singleton for the lifetime // of your application as they don't maintain state. var userClient = clientFactory . GetUserClient ( ) ; var channelClient = clientFactory . GetChannelClient ( ) ; var messageClient = clientFactory . GetMessageClient ( ) ; var reactionClient = clientFactory . GetReactionClient ( ) ; var jamesBond = await userClient . UpsertAsync ( new UserRequest { Id = "james_bond" } ) ; var agentM = await userClient . UpsertAsync ( new UserRequest { Id = "agent_m" } ) ; var channel = await channelClient . GetOrCreateAsync ( "messaging" , "superHeroChannel" , createdBy : jamesBond . Id ) ; await channelClient . AddMembersAsync ( channel . Type , channel . Id , jamesBond . Id , agentM . Id ) ; var message = await messageClient . SendMessageAsync ( channel . Type , channel . Id , jamesBond . Id , "I need a new quest Agent M." ) ; await reactionClient . SendReactionAsync ( message . Id , "like" , agentM . Id ) ;
$ dotnet add package stream-chat-net提示:您可以在样本文件夹中找到代码样本。
using StreamChat . Clients ; // Client factory instantiation.
var clientFactory = new StreamClientFactory ( "YourApiKey" , "YourApiSecret" ) ;
// Or you can configure some options such as custom HttpClient, HTTP timeouts etc.
var clientFactory = new StreamClientFactory ( "YourApiKey" , "YourApiSecret" , opts => opts . Timeout = TimeSpan . FromSeconds ( 5 ) ) ;
// Get clients from client factory. Note: all clients can be used as a singleton in your application.
var channelClient = clientFactory . GetChannelClient ( ) ;
var messageClient = clientFactory . GetMessageClient ( ) ; var userClient = clientFactory . GetUserClient ( ) ;
// Without expiration
var token = userClient . CreateToken ( "bob-1" ) ;
// With expiration
var token = userClient . CreateToken ( "bob-1" , expiration : DateTimeOffset . UtcNow . AddHours ( 1 ) ) ; var userClient = clientFactory . GetUserClient ( ) ;
var bob = new UserRequest
{
Id = "bob-1" ,
Role = Role . Admin ,
Teams = new [ ] { "red" , "blue" } // if multi-tenant enabled
} ;
bob . SetData ( "age" , 27 ) ;
await userClient . UpsertAsync ( bob ) ;
// Batch update is also supported
var jane = new UserRequest { Id = "jane" } ;
var june = new UserRequest { Id = "june" } ;
var users = await userClient . UpsertManyAsync ( new [ ] { bob , jane , june } ) ; var userClient = clientFactory . GetUserClient ( ) ;
await userClient . ExportAsync ( "bob-1" ) ;
await userClient . DeactivateAsync ( "bob-1" ) ;
await userClient . ReactivateAsync ( "bob-1" ) ;
await userClient . DeleteAsync ( "bob-1" ) ; var channelTypeClient = clientFactory . GetChannelTypeClient ( ) ;
var chanTypeConf = new ChannelTypeWithStringCommands
{
Name = "livechat" ,
Automod = Automod . Disabled ,
Commands = new List < string > { Commands . Ban } ,
Mutes = true
} ;
var chanType = await channelTypeClient . CreateChannelTypeAsync ( chanTypeConf ) ;
var allChanTypes = await channelTypeClient . ListChannelTypesAsync ( ) ; var channelClient = clientFactory . GetChannelClient ( ) ;
// Create a channel with members from the start, Bob is the creator
var channel = channelClient . GetOrCreateAsync ( "messaging" , "bob-and-jane" , bob . Id , bob . Id , jane . Id ) ;
// Create channel and then add members, Mike is the creator
var channel = channelClient . GetOrCreateAsync ( "messaging" , "bob-and-jane" , mike . Id ) ;
channelClient . AddMembersAsync ( channel . Type , channel . Id , bob . Id , jane . Id , joe . Id ) ; var messageClient = clientFactory . GetMessageClient ( ) ;
// Only text
messageClient . SendMessageAsync ( channel . Type , channel . Id , bob . Id , "Hey, I'm Bob!" ) ;
// With custom data
var msgReq = new MessageRequest { Text = "Hi june!" } ;
msgReq . SetData ( "location" , "amsterdam" ) ;
var bobMessageResp = await messageClient . SendMessageAsync ( channelType , channel . Id , msgReq , bob . Id ) ;
// Threads
var juneReply = new MessageRequest { Text = "Long time no see!" } ;
var juneReplyMessage = await messageClient . SendMessageToThreadAsync ( channel . Type , channel . Id , juneReply , june . Id , bobMessageResp . Message . Id ) var reactionClient = clientFactory . GetReactionClient ( ) ;
await reactionClient . SendReactionAsync ( message . Id , "like" , bob . Id ) ;
var allReactions = await reactionClient . GetReactionsAsync ( message . Id ) ; var channelClient = clientFactory . GetChannelClient ( ) ;
var userClient = clientFactory . GetUserClient ( ) ;
var flagClient = clientFactory . GetFlagClient ( ) ;
await channelClient . AddModeratorsAsync ( channel . Type , channel . Id , new [ ] { jane . Id } ) ;
await userClient . BanAsync ( new BanRequest
{
Type = channel . Type ,
Id = channel . Id ,
Reason = "reason" ,
TargetUserId = bob . Id ,
UserId = jane . Id
} ) ;
await flagClient . FlagUserAsync ( bob . Id , jane . Id ) ; var permissionClient = clientFactory . GetPermissionClient ( ) ;
await permissionClient . CreateRoleAsync ( "channel-boss" ) ;
// Assign users to roles (optional message)
await channelClient . AssignRolesAsync ( new AssignRoleRequest
{
AssignRoles = new List < RoleAssignment >
{
new RoleAssignment { UserId = bob . ID , ChannelRole = Role . ChannelModerator } ,
new RoleAssignment { UserId = june . ID , ChannelRole = "channel-boss" }
} ,
Message = new MessageRequest { Text = "Bob and June just became mods" , User = bob }
} ) ; var deviceClient = clientFactory . GetDeviceClient ( ) ;
var junePhone = new Device
{
ID = "iOS Device Token" ,
PushProvider = PushProvider . APN ,
UserId = june . ID
} ;
await deviceClient . AddDeviceAsync ( junePhone ) ;
var devices = await deviceClient . GetDevicesAsync ( june . Id ) ; var channelClient = clientFactory . GetChannelClient ( ) ;
var taskClient = clientFactory . GetTaskClient ( ) ;
var taskResponse = channelClient . ExportChannelAsync ( new ExportChannelRequest { Id = channel . Id , Type = channel . Type } ) ;
// Wait for the completion
var complete = false ;
var iterations = 0 ;
AsyncTaskStatusResponse resp = null ;
while ( ! complete && iterations < 1000 )
{
resp = await taskClient . GetTaskStatusAsync ( taskResponse . TaskId ) ;
if ( resp . Status == AsyncTaskStatus . Completed )
{
complete = true ;
break ;
}
iterations ++ ;
await Task . Delay ( 100 ) ;
}
if ( complete )
{
Console . WriteLine ( resp . Result [ "url" ] ) ;
} 我们欢迎代码更改改善该库或解决问题,请确保遵循所有最佳实践,并在GitHub上提交拉动请求之前添加测试,并添加测试。我们很高兴将您的代码合并到官方存储库中。确保首先签署我们的贡献许可协议(CLA)。有关更多详细信息,请参见我们的许可证文件。
前往贡献.md,以获取一些开发技巧。
我们最近关闭了3800万美元的B系列资金回合,我们一直在积极发展。我们的API被超过十亿的最终用户使用,您将有机会对全球最强大的工程师团队中的产品产生巨大影响。
查看我们当前的开口,并通过Stream的网站申请。