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

2010年8月7号----SGS游戏开发之多玩家房间创建

阅读更多
2010年8月7号---SGS游戏开发之多玩家房间创建。
对于SGS游戏多房间开发的东西,昨天和今天进行了学习和处理,完成了房间的创建和多玩家共玩的开发。但是只是基础性的开发,通过Channel绑定到GameRoom,玩家进入后系统自动为其分配房间,每个房间现在只是先两个玩家,玩家进入之后,系统自动根据进入房间的玩家,为其分配房间,当然这里是可以设置的,玩家可以选择自己的房间,初期先这样儿弄:具体简单实现如下:

对于房间的设置:在GameRoom中可以保存Channel对象和一个Map对象,Channel对象保存该房间所使用的通道,Map对象保存玩家的sessionId以及相关的一些信息。这样,对于Channel的建立来说,建立ChannelListener和Channel;建立房间之后,将房间名字绑定到相应的房间对象上去,以便用户访问。

在主程序的initialize()方法中可以建立Channel和相应的房间,并对房间进行名字字符串Binding()设置。然后在loggin()方法中,根据房间满员情况将用户添加到合适的房间中去。在继承自ClientSessionListener的客户端监听程序中,一旦用户反馈准备好了的信息,就根据用户加入的房间,将用户已准备就绪的情况发送给GameRoom对象。GameRoom根据用户已准备情况,决定房间的游戏是否开始执行。

这样就建立了基本的多房间玩家游戏程序的实现。其实,主要的思想就是根据Channel的特质来进行开发。很方便的原因也就是因为SGS或者说RedDwarf就是一款游戏服务端程序开发平台。

Publish/subscribe Channels
As motioned in Lesson1,in addition to client/server communication,the PDS also supports public/subscribe channels. Client/Server communication always has the server and one client on either end of the message;  in contrast, the channel system has N participating clients who can send and receive messages. Clients are made participants of the channel by being joined to it by the server.A client may be joined to many channels at onece.

Server applications are in control of channels. They are the ones who create the channel,add user to it,or remove users from it.Client can send request to the server to be added or removed from channels,and can request that a message be sent to the channel.server application can choose to listen packets sent on the channel to decide if they should be modified or discarded.

Joining and Leavinga 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 chnnels it is a part of and how to communicate on then?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 message on the channel.

Below is an implemention of joinedChannel from our seecond sample client application,HelloChannelClient.This client adds a conbo box to the left of the input field in HelloUserClient so you can select if the input is send directly to the server or to one of the channels the server has joined you to.You can use the server application called HelloChannels to test this client.This server application creats two channels,Foo and Bar,and automatically joins all users to them.
    public ClientChannelListener joinedChannel(ClientChannel channel){
      String channelName = channel.getName();
      channelByName.put(channelName,channel);
      appendOutput("Joined to channel "+channelName);
      c hannelSelectorModel.addElement(channelName);
      return new HelloChannelListener();
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics