Let's introduce today how to use WebSocket under Unity3d.
First introduce what is WebSocket, and the difference and connection with socket, and HTTP, then introduce some open-source projects of WebSocket.
What is WebSocket?
The WebSocket protocol is a communication mechanism based on message frame and TCP, which is introduced in order to realize the network client and server-side full-duplex communication, the goal of this Protocol is to introduce the mechanism of bidirectional communication on the HTTP server, thus overcoming the flaw of HTTP one-way communication ( HTTP design is not intended for two-way communication, it can be multiplexed http port, support HTTP proxy, authentication, etc., although, WebSocket can be independent of HTTP existence. The detailed content can refer to RFC6455 (https://datatracker.ietf.org/doc/rfc6455/) inside has the detailed introduction.
So what is the difference and connection between WebSocket and Http,socket?
- WebSocket and HTTP
In fact, historically speaking, WebSocket is to overcome the HTTP can not be introduced in two-way communication, in the usual use, can be reused HTTP port and function, in addition, they have no other connection, but is completely independent protocol, usually, HTTP is a one-way web services, While WebSocket is full-duplex, the server and the client can transmit information in real time, and they can be deployed simultaneously on the HTTP server when referencing, especially in Nodejs.
- WebSocket and sockets
So what's the relationship between WebSocket and sockets? In fact, it can be understood that WebSocket is implemented on the basis of the socket, which is based on the message frame and TCP protocol, and the socket is more general, in programming, can be selected in TCP,UDP, also need to control the data flow format, each time the length of the data need to control and read.
How to use WebSocket under Unity3d
Now more and more Unity3d games need to use WebSocket or back-office services, in practice, nodejs,socketio more and more as a background service to apply, then the Unity3d front-end can be used in the open-source websocket There are two kinds:
Unitysocketio
You can refer to Https://github.com/kaistseo/UnitySocketIO-WebSocketSharp, which is completely in the way of the C # DLL Unity3d, and tests the use of Windows and Linux completely without problems. Analyze the pros and cons:
Advantages: Direct use of DLLs, no Unity3d dependencies, code better test, support a variety of message types, such as text, JSON and so on.
Cons: The need to rely on third-party libraries, such as Supersocket,simplejson, requires separate maintenance under iOS.
Socket.io for Unity
Unity3d's Asset store has a free open source project Socket.io for Unity (https://www.assetstore.unity3d.com/en/#!/content/21721) can use. Tested for Windows and Linux versions, no problem. Other andriod and iOS should also be no problem, according to the Code and documentation.
Advantages: The code is embedded directly into the Unity3d, there are source code, support the JSON message transmission.
Cons: Writing test cases is relatively cumbersome, the JSON message is simple and does not support JSON parsing of objects, but this block should be rewritten. http://blog.csdn.net/leoleocs/article/details/48824921
Use of WebSocket under Unity3d