`
AquariusM
  • 浏览: 143910 次
  • 性别: Icon_minigender_1
  • 来自: 南阳
社区版块
存档分类
最新评论

2010年8月11号---RedDwarf之Channel通信学习总结

阅读更多

    昨天遇到了一个很神奇的问题,因为有一个客户端的请求,我并没有在服务器端发送相应的请求码,但这个请求却执行了;开始一直以为是服务端的某个地方没有注意,找来找去发现,真是没有发送呀,就在客户端as程序中调试,后来发现这个case是放在紧靠着default的上边的。于是,就换了个位置发现确实不运行了,再考虑一下交互程序的特性,虽然在出问题的这个阶段客户端是处在一个timer()的delay事件中,但是这个时候客户端与服务器还是在通讯中,再具体点客户端的received()方法一直在运行过程中,所以每次都要执行一个default上边的那个case;
    但是再仔细看看才发现,都是因为没有写break的原因,并不是每一次都会执行default的,只有当case里确实没有给定的值时,才会执行default。
    又回到远点,一切都是基础基础很重要一定要扎实,不扎实就会在实践中出问题。
   
    游戏房间与player之间通过channel进行连接,这里进行解析:

  

    ChannelManager cm = AppContext.getChannelManager();

    DataManager dm = AppContext.getDataManager();

    //这里一般是新建一个用户自定义类型的ChannelListener,注意自定义类型ChannelListener必须实现 

    //ChannelListener和Serializable类

    ChannelListener listener1 = new userChannelListener();

    Channel channel1 = cm.createChannel("channelName",listener1,Delivery.RELIABLE);

   

    Room rm = new Room(channel1);

    dm.setBinding("room1",rm);

   

    这样就完成了一个基本的房间创建,Room为自定义类型,每一个Room对象都包含一个Channel对象和一个用户列表,用户列表可以用Collection 容器中的类型定义,根据自己的需求来设定;

    服务端:

    通过Channel.join()方法,将用户添加入Channel中,然后在服务器端,

    实现了ChannelListener的类,需要定义receiveMessage(Channel channel,ClientSession,ByteBuffer message)方法,这样服务器端Channel、客户端ClientSession还有客户端发送到服务器的数据ByteBuffer就可以实现客户端给服务端发送数据了。

     再来看客户端:

     客户端中在SimpleClientListener接口中,会有一个需要实现的方法

     joinedChannel(channel:ClientChannel):ClientChannelListener()

     该方法接受一个ClientChannel类型的对象作为参数。客户端需要保存这个参数,因为需要用这个参数的client()方法,发送数据给服务端;同时该方法的返回值是实现了ClientChannelListener 接口的对象。这个借口提供两个方法,一个receivedMessage和leftChannel方法。receivedMessage方法在服务端Channel发送一个数据包到客户端时调用,来接受服务端channel的消息。leftChannel()顾名思义,就是当客户离开channel时在客户端调用。

     基本上的流程在文档里都有,英文:

     As mentioned above, adding and removing users from the channel is under the control of the server application.How then does a client know what channels it is a part of and how to communicate on them? The answer is another callback on the SimpleClientListener interface: joinedChannel. The joinedChannel callback receives a ClientChannel object as its one parameter. The client application should save this object, since this is its interface for sending messages on the channel.
     The SimpleClient expects this callback to return an object that implements the ClientChannelListener interface. This interface has two methods on it, receivedMessage and leftChannel. The first is called on a callback whenever a packet for this user is received from the channel. The second callback is called if the server  removes this user from the channel.

 

    

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics