서버/node.js

[socket.io] 유니티에서 socket.io 사용하기

JSH1 2022. 1. 3. 18:15

https://github.com/fpanettieri/unity-socket.io-DEPRECATED

 

GitHub - fpanettieri/unity-socket.io-DEPRECATED: Socket.IO client for Unity3d

Socket.IO client for Unity3d. Contribute to fpanettieri/unity-socket.io-DEPRECATED development by creating an account on GitHub.

github.com

 

https://youtu.be/en-MuD63LhE

참고 영상

 

SocketIO폴더를 통째로 유니티 프로젝트에 넣어줌


 

AssemblyInfo.cs 오류

 

20라인 [assembly: AssemblyVersion("1.0.2.*")] 수정 [assembly: AssemblyVersion("1.0.2")]

 

https://github.com/sta/websocket-sharp/issues/621


 

 package.json "socket.io": "^2.4.1", "socket.io-client": "2.4"

socket.io 4.x, 3.x 버전을 사용하면 유니티에서 보낸 통신을 못받고 에러표시도 따로 안뜸
socket.io 버전하고 socket.io-client버전이 비슷하지 않으면 유니티 외에 다른 클라이언트 연결에 문제가 생김


 


서버 var io = require('socket.io')(3000); io.on('connection', function (socket) { console.log('a user connected: ' + socket.id); socket.on('disconnect', function () { console.log('user disconnect: ' + socket.id); }); socket.on('post', position => { console.log(position); }); });

 

클라이언트 using UnityEngine; using SocketIO; using System; public class test : MonoBehaviour { public SocketIOComponent socket = null; void Start() { GameObject go = GameObject.Find("SocketIO"); socket = go.GetComponent<SocketIOComponent>(); } private void Update() { if(Input.GetKeyDown(KeyCode.Space)) { PlayerPosition playerPosition = new PlayerPosition(transform); socket.Emit("post", new JSONObject(JsonUtility.ToJson(playerPosition))); } } } [Serializable] public class PlayerPosition { public Vector3 position; public PlayerPosition(Transform transform) { position = transform.position; } }

 

임시로 만든 test 스크립트 추가


 


 

open이벤트 두 번 실행 문제

        socket.On("open", TestOpen);
    public void TestOpen(SocketIOEvent e)
    {
        Debug.Log("[SocketIO] Open received: " + e.name + " " + e.data);
    }

 

SocketIOComponent.cs 103라인

'서버 > node.js' 카테고리의 다른 글

[vscode] 자동 정렬 설정하기  (0) 2021.12.17
[node.js] npm init  (0) 2021.12.17
[node.js] Error: Cannot find module 'moment'  (0) 2021.12.15