我这里 创建频道成功后,我发布了一个Unity3D.exe程序 到另外一台电脑上,,
首先,换一个userId2222,点击登陆,输入 后面 房间 输入【桃园】,点击加入频道,,没有作用,,
发消息也没有反应.... 如下图:
我的完整代码如下:
using LeanCloud.Realtime;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
public class IMDemoA : MonoBehaviour {
AVRealtime avRealtime;
// AVIMClient liubei = null;
AVIMClient myclient = null;
public InputField inputIMGroup;
public InputField inputPlayerID;
public InputField inputMessage;
public Text systemMsgText;
string newMsg="";
bool isGetNewMsg = false;
//开始初始化
void Start()
{
// 使用 AppId 和 App Key 初始化 SDK
avRealtime = new AVRealtime("4tva9s3odf5lvvb8vrfmockfnhm6lyijhtunuhs9bkmvuwnq", "q4t4ftbh7hnluellzasn6ay4vahxkpjyrjquqlgazhutbjqy");
// 方便调试将 WebSocket 日志打印在 Debug.Log 控制台上
AVRealtime.WebSocketLog(Debug.Log);
}
//登陆按钮触发事件......
public void IMLogin()
{
string PlayerId = inputPlayerID.text;
string gu_id = Guid.NewGuid().ToString("N");
// 新的登录
avRealtime.CreateClientAsync(PlayerId, tag: "Desktop", deviceId: gu_id).ContinueWith(c => {
if (c.IsCompleted)
{
myclient = c.Result;
myclient.OnMessageReceived += OnIMMessageReceived;
myclient.OnMembersJoined += OnIMClientMembersJoined;
NewMsg("IMLogin成功");
// systemMsgText.text += "IMLogin成功";
// myclient.OnInvited 被邀请
//myclient.OnKicked 被踢出
}
});
}
//有新Client加入的事件触发方法
private void OnIMClientMembersJoined(object sender, AVIMOnMembersJoinedEventArgs e)
{
// throw new NotImplementedException();
string msg = "";
foreach ( string userId in e.JoinedMembers)
{
msg += "id : " + userId + " -- ";
}
NewMsg(msg);
// systemMsgText.text += msg;
Debug.Log(msg);
}
//收到新消息的方法
private void OnIMMessageReceived(object sender, AVIMMessageEventArgs e)
{
// throw new NotImplementedException();
// systemMsgText.text += msg;
// Debug.Log(msg);
if (e.Message is AVIMTextMessage)
{
var textMessage = (AVIMTextMessage)e.Message;
// textMessage.ConversationId 是该条消息所属于的对话 Id
// textMessage.TextContent 是该文本消息的文本内容
// textMessage.FromClientId 是消息发送者的 client Id
string msg = string.Format("你收到来自于 Id 为 {0} 的对话的文本消息,消息内容是: {1},发送者的 client Id 是 {2}", textMessage.ConversationId, textMessage.TextContent, textMessage.FromClientId);
// systemMsgText.text += msg;
NewMsg(msg);
Debug.Log(msg);
}
}
//创建频道按钮触发事件...
public void CreateIMGroup()
{
string groupName = inputIMGroup.text;
// 以关羽、张飞的游戏 ID 作为受邀进入对话的成员列表
//var memberList = new List<string>()
//{
// "1002",
// "1003"
//};
// 以「桃园」作为帮派的名称
var name = groupName;// "桃园";
// 以「桃园」作为名称,以关羽、张飞作为成员,建立对话
// 对话的创建者刘备(client id:1001)会默认加入对话,因此无需重复添加到 memberList 里面去
// liubei.CreateConversationAsync( name: name, members: memberList ).ContinueWith(s =>
myclient.CreateConversationAsync( name: name ,isSystem:true ).ContinueWith(s =>
{
Debug.LogError("CreateConversationAsync ......");
if( s.Result != null)
{
NewMsg("CreateIMGroup成功");
Debug.LogError("CreateConversationAsync : " + s.Result.Name );
}
if (s.IsCompleted)
{
//systemMsgText.text += "CreateIMGroup成功";
NewMsg("CreateIMGroup成功");
Debug.Log("CreateConversationAsync set memberList OK");
}
else
{
NewMsg("CreateIMGroup失败了...");
Debug.Log("CreateIMGroup失败了");
}
});
}
AVIMConversation imConversation;
//加入频道 按钮点击触发事件...
public void SetJoinGroup()
{
Debug.Log("SetJoinGroup" );
string userId = inputPlayerID.text;
string IMGroupNumber = inputIMGroup.text;
myclient.GetConversationAsync(IMGroupNumber, true ).ContinueWith(s =>
{
if (s.IsCompleted)
{
if( s.Result != null)
{
Debug.LogError(" GetConversationAsync Name is ...... : " + s.Result.Name);
imConversation = s.Result;
NewMsg(" GetConversationAsync Name is ...... : " + s.Result.Name);
}
Debug.Log(" GetConversationAsync IsCompleted......");
}
else
{
Debug.Log("IsCompleted失败了......");
}
}).
ContinueWith(y =>
{
myclient.JoinAsync(imConversation).ContinueWith(x =>
{
if (x.IsCompleted)
{
Debug.Log("加入某个频道成功......");
//systemMsgText.text += "加入某个频道成功......";
NewMsg("加入某个频道成功......");
}
});
});
}
//发送消息按钮触发事件....
public void SendMsgToIMGroup()
{
string number = inputIMGroup.text;
string msg = inputMessage.text;
// 「桃园」是一个 AVIMConversation 实例
var TaoYuanConversation = myclient.GetConversationAsync(number, true).ContinueWith(s =>
{
// 创建一个文本消息
var textMessage = new AVIMTextMessage( msg);
// 将该文本消息发送出去
s.Result.SendMessageAsync(textMessage).ContinueWith(x =>
{
if (x.IsCompleted)
{
// systemMsgText.text += "信息已经发送....";
NewMsg("信息已经发送....");
}
});
});
}
//测试频道按钮触发事件......
public void GetAllConversation()
{
Debug.Log("SetJoinGroup");
string userId = inputPlayerID.text;
string IMGroupNumber = inputIMGroup.text;
myclient.GetQuery().FirstAsync().ContinueWith(s =>
{
Debug.Log("获取频道成功......" + s.Result.Name );
//systemMsgText.text += "加入某个频道成功......";
NewMsg("获取频道成功......" + s.Result.Name);
}
);
}
//程序退出的方法...关闭客户端
private void OnApplicationQuit()
{
//liubei.CloseAsync();
myclient.CloseAsync().ContinueWith(s =>
{
Debug.Log("myclient 已经关闭......");
});
avRealtime = null;
// liubei = null;
}
// Update is called once per frame
void Update () {
CheckIsGetNewMsg();
}
//由于只能在主线程调用 UI ,,所以有这个方法存在,用于在Update里面判断
void CheckIsGetNewMsg()
{
if (isGetNewMsg)
{
systemMsgText.text +="---- "+ newMsg;
isGetNewMsg = false;
newMsg = "";
}
}
//用户显示新的字符串
void NewMsg(string msg)
{
newMsg = msg;
isGetNewMsg = true;
}
//
}
哪里代码写的有错误还是? 求助......QQ:2360450496,邮箱2360450496@qq.com