クライアント側で問題があります(フレックスを使用しています)。ユーザーが切断して再接続すると、共有オブジェクトで null オブジェクト参照が取得されます。共有オブジェクトを再作成 (再初期化) する必要がありますか?
どんな助けでも大歓迎です。
問題は、2 人のユーザーがログインしてテキスト入力にデータを挿入し、送信をクリックすると、データが問題なく同期されることです。しかし、ユーザーの 1 人が切断して再接続すると、そのユーザーは (更新された) データを同期できなくなります。
以下はサンプルコードです
Main.asc
var user_array=Array();
var obj;
var obj3;
var obj1;
application.onAppStart= function()
{
trace("OnAppStart");
}
application.onConnect = function(client, user_name)
{
application.acceptConnection(client);
monitorConnection(client);
var so1=SharedObject.get("hello");
client.name=user_name;
trace("OnConnect");
obj=client;
user_array.push(user_name);
so1.setProperty("names",user_array);
obj1=user_array;
// callthiss(obj);
client.sendSomething = function(name)
{
for(var a=0; a<application.clients.length; a++)
{
//for(var j=0;j<user_array.length;j++)
//{
if(application.clients[a].name == name)
{
application.clients[a].call("receiveSomething",null,name);
}
//}
}
}
client.Onclick=function(name)
{
var so2=SharedObject.get("so2");
so2.setProperty("so2",name);
}
};
clientPingResponder = function(client) {
this.onResult = function(res) {
// trace("ClientPingResponder.onResult Received: " + res);
clearInterval(client.cleanupTaskId);
}
this.onStatus = function(info) {
//trace("ClientPingResponder.onStatus Received: " + info);
}
};
monitorConnection = function(client) {
client.isAlive = function() {
trace("client.isAlive has been fired");
client.cleanupTaskId = setInterval(client.dead, 2 * 1000);
client.call("pings", new clientPingResponder(client));
}
client.dead = function() {
// trace("cleanupTaskId: " + client.cleanupTaskId);
// trace("monitorTaskId: " + client.monitorTaskId);
clearInterval(client.cleanupTaskId);
clearInterval(client.monitorTaskId);
trace("ghost connection has been detected");
application.disconnect(client);
}
client.monitorTaskId = setInterval(client.isAlive, 7*1000);
trace("detected");
}
application.onDisconnect=function(client)
{
trace("DisConnect"+client.name);
// trace(client.name);
}
クライアント側コード(Flex)
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
<mx:Script>
<![CDATA[
internal var nc:NetConnection;
internal var so1:SharedObject;
internal var so2:SharedObject;
internal var str:Responder=new Responder(str1);
internal function on_login():void
{
if(username!=null)
{
nc=new NetConnection();
nc.client=this;
nc.connect("rtmp://localhost/Sample_2",username.text);
nc.addEventListener(NetStatusEvent.NET_STATUS, netstatusHandler);
}
}
internal function netstatusHandler(e:NetStatusEvent):void
{
switch(e.info.code)
{
case "NetConnection.Connect.Success":
status_1.text="Connected";
trace("so1:: "+so1);
if(so1==null)
{
create_SO();
}
break;
case "NetConnection.Connect.Rejected":
status_1.text="Rejected";
break;
case "NetConnection.Connect.Failed":
status_1.text="Failed";
break;
case "NetConnection.Connect.AppShutdown":
status_1.text="AppShutDown";
break;
case "NetConnection.Connect.NetworkChange":
status_1.text="network Change";
break;
case "NetConnection.Connect.Closed":
status_1.text="Disconnected";
break;
}
}
public function pings():void
{
trace("a ping call from the server has been received");
}
internal function create_SO():void
{
so1=SharedObject.getRemote("hello",nc.uri,false);
so1.connect(nc);
so1.addEventListener(SyncEvent.SYNC, on_hello);
so2=SharedObject.getRemote("so2",nc.uri,false);
so2.connect(nc);
so2.addEventListener(SyncEvent.SYNC, so2Sync);
}
internal var ar:Array=new Array();
internal function on_hello(e:SyncEvent):void
{
var d:*=e.target.data;
for(var s:* in d)
{
trace("SYNC::: "+d[s]);
ar= d[s] as Array;
}
}
internal function str1(strr:String):void
{
trace("str: "+strr);
}
public function onclient():void
{
trace("ss: ");
}
public function onclicka():void
{
for(var i:int=0;i<ar.length;i++)
{
if(ar[i]!=username.text)
{
trace("CALL SEND SOME: "+ar[i]);
nc.call("sendSomething",null,ar[i]);
}
}
}
//public function receiveSomething(str:String):String
public function receiveSomething(stsr:String):String
{
trace("receiveSomething: "+stsr);
rec.text="receiveSomething: "+str;
return stsr;
}
public function ondisconnect():void{
nc.close();
}
public function onreconnect():void
{
nc.connect("rtmp://localhost/Sample_2",username.text);
}
public function ButtonClick(event:MouseEvent):void
{
nc.call("Onclick",null,sendtxt.text);
}
public function so2Sync(event:SyncEvent):void
{
var output=event.target.data;
var a:Array= new Array();
for(var n in output)
{
var obj=output[n];
a.push(obj);
dg.dataProvider=a;
}
}
]]>
</mx:Script>
<mx:TextInput x="10" y="10" id="username"/>
<mx:Button x="188" y="10" label="Login" id="login" click="on_login()"/>
<mx:Button id="Dis" label="Disconnect" click="ondisconnect()" y="40"/>
<mx:Button id="re" label="reconnect" click="onreconnect()" y="70"/>
<mx:Text x="379" y="12" text="Text" id="status_1"/>
<mx:Text x="289" y="12" text="Status"/>
<mx:TextInput x="438" y="180" id="sendtxt"/>
<mx:Button x="632" y="180" label="Send" click="ButtonClick(event)"/>
<mx:List x="434" y="222" id="dg"></mx:List>
</mx:Application>