pomelo-cn
pomelo-cn copied to clipboard
使用Unitysocketio ,停止播放后unity3d无响应
在项目中使用unitysocketio进行客户端和服务器端的通信,能够胜利通信,首先感谢pomelo的这个unitysocketio类库! 我在使用过程中经常遇到当停止播放后,unity就卡死无响应了 我的代码如下,在程序退出时也对资源进行释放 public static Client socket;//网络连接 //程序的入口 void Start() { InitPrefab.InitWelcomePrefab(); //本地服务器 string urlLocal = "http://192.168.1.107:9000"; //远程服务器
socket = new Client(urlLocal);
socket.Opened += new EventHandler(client_Opened);
socket.Message += new EventHandler<MessageEventArgs>(client_Message);
socket.SocketConnectionClosed += new EventHandler(client_SocketConnectionClosed);
socket.Error += new EventHandler<ErrorEventArgs>(client_Error);
socket.On("connect", (fn) =>
{
// socket.Send("[25]");
});
socket.Connect();
}
void client_Error(object sender, ErrorEventArgs e)
{
print("client error!" + e.Message);
throw new NotImplementedException();
}
void client_SocketConnectionClosed(object sender, EventArgs e)
{
print("Connection closed!" + e.ToString());
throw new NotImplementedException();
}
void client_Opened(object sender, EventArgs e)
{
print("connection opened!");
throw new NotImplementedException();
}
//当应用程序退出时 void ApplicationQuit() { if (socket != null) { socket.Close(); } }
每次只能播放一次unity,然后unity就无响应了,很不解,向大家请教
这是资源释放的问题,在退出的时候重写OnApplicationQuit方法,在该方法里面释放资源……
资源释放,我这样写的OnApplicationQuit,把socket关闭,unity还是只能运行一次就卡死,无响应。 //void OnApplicationQuit() //{ // print("public OnApplicationQuit"); // if (socket != null) // { // socket.Close(); // } //}
// On quit
public static void OnApplicationQuit()
{
print("static OnApplicationQuit");
if (socket != null)
{
socket.Close();
}
}
求指教
"每次只能播放一次unity,然后unity就无响应了",这句话怎么理解,一次unity? 程序卡住有没有报异常?能说下你的应用场景么…… 还有你在client_Opened, client_SocketConnectionClosed方法里面抛出的异常有没有捕获。
就是每次当做好场景之后,不是要在unity中进行播放看效果吗,播放一次,点击暂停,然后就卡死了,就是点击这个播放。
socket连接打开和关闭时,我这样处理的:
//连接打开 void client_Opened(object sender, EventArgs e) { print("connection opened!"); throw new NotImplementedException(); } //socket 连接关闭 void client_SocketConnectionClosed(object sender, EventArgs e) { print("Connection closed!" + e.ToString()); throw new NotImplementedException(); }
你把这两个异常去掉试试,建议你做个简单的demo,编译后再运行,因为你这样暂停调用,不会主动释放资源……你可以看看这个demo: https://github.com/NetEase/pomelo-unitychat.
这个Demo,我下载下来了,我的是windows环境,所以后台pomelo,没有运行起来,pomelo-unitychat中的三个CS文件,我看了一次,我把它编译运行看一下还会不会卡死,在unity中暂停就会卡死。
嗯,你按照这个demo的做法可以做一个简单的demo,在退出或者关闭的时候记得释放资源就行了。如果你想运行这个demo,记得用https://github.com/NetEase/chatofpomelo 这个服务端。
嗯,好的,我晚上的时候,把pomelo chat的windows环境搭建起来,运行,调试调试,读读代码,谢谢你啦!
请问pomelo服务器关闭后会触发哪个函数,现在想做服务器关闭后将cache信息保存功能