Android实时游戏字节信息传输

时间:2017-05-25 06:46:43

标签: java android google-play-services google-play-games

我开发自己的游戏,并使用谷歌玩游戏服务来添加多人游戏功能。

我的数据邮件传输有问题。默认情况下,消息对象是byte [],并且它很容易传输对象,如int,char,并将它们转换回来。

我的问题是转移我创建的对象。

工作示例:

发送数据时:

 int a = 0;
 mMsgBuf[0] = (byte) a;

回归时:

int a = (int) mMsgBuf[0];

现在我想发送我的对象,但我需要将他转移到一个字节(不是字节数组),并将他放入mMsgBuf [X]。

我该怎么做?

修改 它很容易将对象转换为byte [],传输的消息是一个byte []。 我正在咨询如何使用我的objectbyte []传输消息(我们知道 - byte []),也许合并两个数组?

1 个答案:

答案 0 :(得分:0)

如果不是,则使您的自定义对象可序列化。 Implement Serializable Interface。 然后在对象和字节数组之间进行转换以传输或接收数据。

对象到byte []:

<script type="text/javascript">
            function validateform() {
                var num = document.add_form.required_number.value;

                var quota = document.add_form.quota.value;


                if(num>quota) {
                    alert("Required Number Exceeds Demand Quota! The Allowed Quota is " + quota);
                    return false;
                }
                else {
                    alert("Error " + num + " " + quota);
                    return false;
                }
            }
        </script>

byte []到Object:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(yourObject);
byte[] yourBytes = bos.toByteArray();
相关问题