赞
踩
1.本次根据需求制作一个小程序发消息到Unity3d互动大屏显示的案例,先上效果:
小程序Unity
2.使用SpringBoot写一个Socket通信,用于监听unity3d客户端的连接以及,向unity3d进行消息的转发:
代码如下:
- public class SocketIOServiceImpl implements ISocketIOService{
- /**
- * 存放已连接的客户端
- */
- private static Map<String, SocketIOClient> clientMap = new ConcurrentHashMap<>();
-
- /**
- * 自定义事件`push_data_event`,用于服务端与客户端通信
- */
- private static final String PUSH_DATA_EVENT = "push_data_event";
-
- @Autowired
- private SocketIOServer socketIOServer;
-
- /**
- * Spring IoC容器创建之后,在加载SocketIOServiceImpl Bean之后启动
- */
- @PostConstruct
- private void autoStartup() {
- start();
- }
-
- @PreDestroy
- private void autoStop() {
- stop();
- }
-
- @Override
- public void start() {
- // 监听客户端连接
- socketIOServer.addConnectListener(client -> {
- System.out.println("************ 客户端: " + getIpByClient(client) + " 已连接 ************");
- // 自定义事件`connected` -> 与客户端通信 (也可以使用内置事件,如:Socket.EVENT_CONNECT)
- client.sendEvent("connected", "你成功连接上了哦...");
- String userId = getParamsByClient(client);
- System.out.println(userId);
- if (userId != null) {
- clientMap.put(userId, client);
- }
- });
-
- // 监听客户端断开连接
- socketIOServer.addDisconnectListener(client -> {
- String clientIp = getIpByClient(client);
- System.out.println(clientIp + " *********************** " + "客户端已断开连接");
- client.sendEvent("disconnect", "你成功断开连接了哦...");
- String userId = getParamsByClient(client);
- if (userId != null) {
- clientMap.remove(userId);
- client.disconnect();
- }
- });
-
- // 自定义事件`client_info_event` -> 监听客户端消息
- socketIOServer.addEventListener(PUSH_DATA_EVENT, String.class, (client, data, ackSender) -> {
- // 客户端推送`client_info_event`事件时,onData接受数据,这里是string类型的json数据,还可以为Byte[],object其他类型
- String clientIp = getIpByClient(client);
- System.out.println(clientIp + " ************ 客户端:" + data);
- socketIOServer.getNamespace("").getBroadcastOperations().sendEvent(PUSH_DATA_EVENT,data);
- });
-
- // 启动服务
- socketIOServer.start();
- }
-
- @Override
- public void stop() {
- if (socketIOServer != null) {
- socketIOServer.stop();
- socketIOServer = null;
- }
- }
-
- @Override
- public void pushMessageToUser(String userId, String msgContent) {
- SocketIOClient client = clientMap.get(userId);
- if (client != null) {
- client.sendEvent(PUSH_DATA_EVENT, msgContent);
- }
- }
3.在unity3d书写一个Socket连接服务器端,开启一个socket线程实时接收服务器端转发过来的小程序消息,(IP,端口号)
- var uri = new Uri("http://127.0.0.1:8888");
- SocketIOUnity socket = new SocketIOUnity(uri, new SocketIOOptions
- {
- Query = new Dictionary<string, string>
- {
- {"token", "UNITY" }
- }
- ,
- Transport = SocketIOClient.Transport.TransportProtocol.WebSocket
- });
-
- socket.JsonSerializer = new NewtonsoftJsonSerializer();
- socket.On("connected", (response) =>
- {
- var obj = response.GetValue<string>();
- msg = obj;
- Debug.Log(obj);
- });
- socket.On("push_data_event", (response) =>
- {
- var obj = response.GetValue<string>();
- //Debug.Log(obj);
- msg = obj;
- resMsg = true;
- });
- socket.Connect();
4.服务器和unity3d完成通信之后,在springboot里面为小程序书写一个接口,用于小程序传递消息调用:
- import com.mo.serverdemo.service.SocketIOServiceImpl;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/unity")
- public class MessageToUnityController {
-
- @Autowired
- private SocketIOServiceImpl socketIOService;
-
-
- @GetMapping("/send")
- public String forwardMessage(@RequestParam("message") String message) {
- socketIOService.brocastMessage(message);
- return "ok";
- }
- }
5.小程序调用此接口,进行消息的传递:
6.调用成功后既可进行实时消息发送与接收。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。