使用示例

示例

Ncloudchat React 示例

完整的源码已经公开在 GitHub 上,您可以下载 GitHub 上的源码,也可以通过 https://www.ncloudchat.com 进行使用。

完整的示例代码

以下是访问、创建频道以及向创建的频道发送加入消息的示例代码:


// 初始化
CloudChat nc = CloudChat.GetInstance();
await nc.initialize([PROJECT_ID]);
nc.dispatcher.onMessageReceived += e =>
{
    Console.WriteLine("received a new message: ", e);
};
await nc.Connect(
    userId: 'guest@company',
    name: 'Guest',
    profile: 'https://image_url',
    customField: 'json',
);
// 创建频道
var channel = await nc.createChannel(new CloudChatSDK.Channel
{
    name = "New Channel",
    type = "PUBLIC",    // PUBLIC or PRIVATE
    customField = "customField"
});
var channel = await nc.createChannel({type:'PUBLIC', name:'First Channel', customField:'customField'});
var channel_id = channel.createChannel.channel.id.ToString();
// 订阅频道
await nc.subscribe(channel_id);
// 发送消息
var response = await nc.sendMessage(
        channelId: channel_id, 
        type:"text", 
        content: message
    );

Last updated