Unity3D에서 Node.js로 데이터를 protobuf를 사용해서 Websocket으로 전송하기

요약

Unity3D와 Node.JS의 protobuf를 이용해 Websocket으로 통신하는 예제가 없어 만들어 보았다.


설명하는데 복잡한 감이 있어, 앞으로 하게될 내용을 간략히 추려놓았다.

1. websocket 라이브러리 설치

2. protobuf 라이브러리 설치

3. sample idl작성

4. sample idl을 node.js과 c#용으로 컨버팅

5. 서버와 클라이언트 코드를 작성해 테스트 해보기


준비

Unity3D on Windows

websocket

1) Websocket 라이브러리를 다운로드

2) 다운받은 라이브러리를 release 모드로 컴파일

3) \websocket-sharp\bin\release\websocket-sharp.dll 파일을 유니티 프로젝트에 복사


protobuf

1) .net용 protobuf 라이브러리 다운로드

2) \protobuf-net r668\Full\unity\protobuf-net.dll 파일을 유니티 프로젝트에 복사


Node.js on CentOS

# Node.js에 모듈 설치 $ npm install websocket $ npm install protobuf # Python 2.7.5 설치 $ wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz $ tar xfz Python-2.7.5.tgz $ cd Python-2.7.5 $ ./configure && make && make install # Protocol Buffer 설치 $ wget https://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz $ tar xfz protobuf-2.5.0.tar.gz $ cd protobuf-2.5.0 $ ./configure && make && make install


샘플 코드

1. protobuf idl 작성하기

sample idl을 feeds.proto로 저장


2. 작성한 idl 컨버팅

node.js

protoc파일은 리눅스용 protobuf 라이브러리가 소스형태라, 컴파일 해야 하며, 컴파일이 성공했다면 생성된다.

$ /usr/local/bin/protoc --descriptor_set_out=feeds.desc --include_imports feeds.proto

생성된 파일을 Node.js 샘플 서버 코드가 있는 경로에 복사

생성된 파일 내용

c#

protogen파일은 라이브러리에 포함되어 있다.

\protobuf-net r668\ProtoGen.exe -i:feeds.proto -o:Feeds.cs

생성된 파일을 유니티 프로젝트에 복사

생성된 파일 내용

3. 클라이언트와 서버 코드 작성하기

unity sample client script

node.js sample server code


테스트 해보기

node.js log

$ ./node test.js Thu May 29 2014 17:20:13 GMT+0900 (KST) Server is listening on port 8080 of process:3709 Thu May 29 2014 17:20:18 GMT+0900 (KST) Connection accepted.3709 Server Received Binary Message of 9 bytes hello

Unity3D log

Laputa says: hello