首先A进行通话请求的逻辑

  • 将A的手机号和B的手机号封装成一个实体类,通过socket发送给服务器
  1. 首先封装成一个实体类
  2. XingeChatMessage xingeChatMessageHome = new XingeChatMessage();
  3. xingeChatMessageHome.setMasterPhone(MasterPhone);
  4. xingeChatMessageHome.setSlavePhone(SlavePhone);
  1. 然后将上面封装的实体类基础上,增加一个类型,进行一下序列化,再封成MessageMain,再将MessageMain序列化后建立socket,通过socket发送出去
  2. MessageMain xinSendMessageMain = new MessageMain();
  3. xinSendMessageMain.setMessageType(MessageType.XINGE_MESSAGE);
  4. xinSendMessageMain.setData(ProtoBufUtil.serializer(xingeChatMessage));
  5. byte[] serializer = ProtoBufUtil.serializer(xinSendMessageMain);
  6. dataOutputStream.writeInt(serializer.length);
  7. dataOutputStream.write(serializer);
  8. dataOutputStream.flush();
  • 发送了双方手机号后就一直监听服务器返回的消息,如果收到的消息进行反序列化后,得到的类型是MessageType.OPEN_CONNECTION,就可以发送视频和音频消息了
  1. MessageMain deserializer = ProtoBufUtil.deserializer(receive_buffer,MessageMain.class);
  2. if (deserializer.getMessageType().equals(MessageType.OPEN_CONNECTION)) {

B接收通话的逻辑

  • 首先接收到推送过来的消息message,Json解析后获取UUID,将UUID封装成XingeChatMessage,类似A的操作,将XingeChatMessage添加上类型,进行序列化,封装成MessageMain类型,建立socket连接,通过socket发送给服务器
  1. XingeClientMessage xin = JSON.parseObject(message,XingeClientMessage.class);
  2. XingeChatMessage xingeChatMessage = new XingeChatMessage();
  3. xingeChatMessage.setChatGroupUUID(xin.getChatGroupId());
  1. MessageMain xinSendMessageMain = new MessageMain();
  2. xinSendMessageMain.setMessageType(MessageType.XINGE_MESSAGE);
  3. xinSendMessageMain.setData(ProtoBufUtil.serializer(xingeChatMessage));
  4. byte[] serializer = ProtoBufUtil.serializer(xinSendMessageMain);
  5. dataOutputStream.writeInt(serializer.length);
  6. dataOutputStream.write(serializer);
  7. dataOutputStream.flush();

发送接收视频(图片)

  • 所有的图片数据需要封装成MessageMain,然后加上类型,进行序列化后发送出去
  1. MessageMain videoSendMessageMain = new MessageMain();
  2. videoSendMessageMain.setMessageType(MessageType.IMAGE);
  3. videoSendMessageMain.setData(grayData);
  4. byte[] imageSerializer = ProtoBufUtil.serializer(videoSendMessageMain);
  5. byte[] lengthByteArray = toolUtils.intToBytes2(imageSerializer.length);
  6. dataOutputStream.write(toolUtils.addBytes(lengthByteArray, imageSerializer));
  7. dataOutputStream.flush();
  • 接收到的数据,首先接收数据长度,根据长度读取byte[],然后反序列化,判断是什么类型,如果是图片,显示出来;如果是音频就播放出来
  1. int singleLength = dataInputStream.readInt();
  2. byte[] receive_buffer = new byte[singleLength];
  3. toolUtils.readData(dataInputStream, receive_buffer);
  4. MessageMain videoReceiveMessageMain = ProtoBufUtil.deserializer(receive_buffer, MessageMain.class);
  5. if (videoReceiveMessageMain.getMessageType() == MessageType.IMAGE)
  6. if (videoReceiveMessageMain.getMessageType() == MessageType.VOICE)

发送接收音频

  • 所有的音频数据需要封装成MessageMain,然后加上类型,进行序列化后发送出去
  1. MessageMain audioSendMessageMain = new MessageMain();
  2. audioSendMessageMain.setMessageType(MessageType.VOICE);
  3. audioSendMessageMain.setData(audioRecordBuffer);
  4. byte[] audioSerializer = ProtoBufUtil.serializer(audioSendMessageMain);
  5. byte[] lengthByteArray = toolUtils.intToBytes2(audioSerializer.length);
  6. dataOutputStream.write(toolUtils.addBytes(lengthByteArray, audioSerializer));
  7. dataOutputStream.flush();
文档更新时间: 2019-02-19 10:49   作者:LMY