当前位置:   article > 正文

微信小程序与Unity3d实时通信_easyar 微信小程序 unity3d

easyar 微信小程序 unity3d

1.本次根据需求制作一个小程序发消息到Unity3d互动大屏显示的案例,先上效果:

小程序Unity

2.使用SpringBoot写一个Socket通信,用于监听unity3d客户端的连接以及,向unity3d进行消息的转发:

代码如下:

  1. public class SocketIOServiceImpl implements ISocketIOService{
  2. /**
  3. * 存放已连接的客户端
  4. */
  5. private static Map<String, SocketIOClient> clientMap = new ConcurrentHashMap<>();
  6. /**
  7. * 自定义事件`push_data_event`,用于服务端与客户端通信
  8. */
  9. private static final String PUSH_DATA_EVENT = "push_data_event";
  10. @Autowired
  11. private SocketIOServer socketIOServer;
  12. /**
  13. * Spring IoC容器创建之后,在加载SocketIOServiceImpl Bean之后启动
  14. */
  15. @PostConstruct
  16. private void autoStartup() {
  17. start();
  18. }
  19. @PreDestroy
  20. private void autoStop() {
  21. stop();
  22. }
  23. @Override
  24. public void start() {
  25. // 监听客户端连接
  26. socketIOServer.addConnectListener(client -> {
  27. System.out.println("************ 客户端: " + getIpByClient(client) + " 已连接 ************");
  28. // 自定义事件`connected` -> 与客户端通信 (也可以使用内置事件,如:Socket.EVENT_CONNECT)
  29. client.sendEvent("connected", "你成功连接上了哦...");
  30. String userId = getParamsByClient(client);
  31. System.out.println(userId);
  32. if (userId != null) {
  33. clientMap.put(userId, client);
  34. }
  35. });
  36. // 监听客户端断开连接
  37. socketIOServer.addDisconnectListener(client -> {
  38. String clientIp = getIpByClient(client);
  39. System.out.println(clientIp + " *********************** " + "客户端已断开连接");
  40. client.sendEvent("disconnect", "你成功断开连接了哦...");
  41. String userId = getParamsByClient(client);
  42. if (userId != null) {
  43. clientMap.remove(userId);
  44. client.disconnect();
  45. }
  46. });
  47. // 自定义事件`client_info_event` -> 监听客户端消息
  48. socketIOServer.addEventListener(PUSH_DATA_EVENT, String.class, (client, data, ackSender) -> {
  49. // 客户端推送`client_info_event`事件时,onData接受数据,这里是string类型的json数据,还可以为Byte[],object其他类型
  50. String clientIp = getIpByClient(client);
  51. System.out.println(clientIp + " ************ 客户端:" + data);
  52. socketIOServer.getNamespace("").getBroadcastOperations().sendEvent(PUSH_DATA_EVENT,data);
  53. });
  54. // 启动服务
  55. socketIOServer.start();
  56. }
  57. @Override
  58. public void stop() {
  59. if (socketIOServer != null) {
  60. socketIOServer.stop();
  61. socketIOServer = null;
  62. }
  63. }
  64. @Override
  65. public void pushMessageToUser(String userId, String msgContent) {
  66. SocketIOClient client = clientMap.get(userId);
  67. if (client != null) {
  68. client.sendEvent(PUSH_DATA_EVENT, msgContent);
  69. }
  70. }

3.在unity3d书写一个Socket连接服务器端,开启一个socket线程实时接收服务器端转发过来的小程序消息,(IP,端口号)

  1. var uri = new Uri("http://127.0.0.1:8888");
  2. SocketIOUnity socket = new SocketIOUnity(uri, new SocketIOOptions
  3. {
  4. Query = new Dictionary<string, string>
  5. {
  6. {"token", "UNITY" }
  7. }
  8. ,
  9. Transport = SocketIOClient.Transport.TransportProtocol.WebSocket
  10. });
  11. socket.JsonSerializer = new NewtonsoftJsonSerializer();
  12. socket.On("connected", (response) =>
  13. {
  14. var obj = response.GetValue<string>();
  15. msg = obj;
  16. Debug.Log(obj);
  17. });
  18. socket.On("push_data_event", (response) =>
  19. {
  20. var obj = response.GetValue<string>();
  21. //Debug.Log(obj);
  22. msg = obj;
  23. resMsg = true;
  24. });
  25. socket.Connect();

4.服务器和unity3d完成通信之后,在springboot里面为小程序书写一个接口,用于小程序传递消息调用:

  1. import com.mo.serverdemo.service.SocketIOServiceImpl;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. @RequestMapping("/unity")
  9. public class MessageToUnityController {
  10. @Autowired
  11. private SocketIOServiceImpl socketIOService;
  12. @GetMapping("/send")
  13. public String forwardMessage(@RequestParam("message") String message) {
  14. socketIOService.brocastMessage(message);
  15. return "ok";
  16. }
  17. }

5.小程序调用此接口,进行消息的传递:

 6.调用成功后既可进行实时消息发送与接收。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/669297
推荐阅读
相关标签
  

闽ICP备14008679号