Socket.io for Unity plug-in usage sharing

     Step 1: Set up the server

        Before use, you need to have a server that supports Socket.io. I refer to the original blog https://blog.csdn.net/nicepainkiller/article/details/80748752 to build it. The server in the blog is implemented using Socket.IO + Node.js, and the container is implemented using express.

Download tool:    Download Node.js  

After entering, you can see the following interface:

Download 32-bit or 64-bit for your own computer. I am using Windows system. You can install the corresponding system installation package for other systems.

I downloaded the node-v10.16.0-x64.msi file and click Run to install it.

Next step:

You can have a default path or a customized path.

Next step

Next step, installation, is finally done.

Then open the cmd window and enter node -v to check whether the installation is successful. As shown in the figure below, the installation is successful.

As shown in the picture above, npm package manager and Online doucumentation shortcuts Add to Path will be installed together.

cmd window, enter npm -v to check whether the node package manager is successfully installed.

Create an empty folder as shown below:

Then cd to the file directory:

Enter npm install socket.io to install socket.io

 

Enter npm install express to install express

 

 

Step 2: Client Testing

        After installation, find the path F:\_SocketIO\Node_JS\node_modules\express;

       Start Unity and import the plug-in Socket.IO for Unity, which is free on the APPStore.

Find beep.js under SocketIO/Server, decompress it, and put the decompressed beep.js script in the express folder.

Then enter in the cmd window: node F:\_SocketIO\Node_JS\node_modules\express\beep.js and press Enter to execute.

Start the case scenario of Unity, with the following output, test the connection between the client and the server.

Server modified script:

var io = require('socket.io')({
	transports: ['websocket'],
});

io.attach(4567);
console.log('Server Start....');
io.on('connection', function(socket){
	 console.log('Client Contect.');
	 socket.on('beep', function(){		
		socket.emit('boop',{ hi: 'Hello,world' });
	 });
	 socket.on('msg', function (data) {
        console.log(data);
    });
})

run:

Client runs:

 

 

Guess you like

Origin blog.csdn.net/hemiaoyuan1989/article/details/96436638