Giter Club home page Giter Club logo

unetllapiwrapper's Introduction

Unet LLAPI Wrapper

This is a small wrapper for the Low Level API functionality in the Unet release for Unity3D 5.6.x

Usage

Just drop the Net folder into your project and make the calls required as shown below. Note, this class exists in the ~Examples folder.

using System.Text;

using UnityEngine;
using UnityEngine.Networking;

namespace LowLevelNetworking
{
	public class NetworkingTest : MonoBehaviour
	{

		NetServer mServer;
		NetClient mClient;

		void Start () {

			var config = new ConnectionConfig();
			config.AddChannel(QosType.Reliable);

			NetManager.mConnectionConfig = config;

			NetManager.Init ();

			mServer = NetManager.CreateServer( 10 , 7777 );
			mClient = NetManager.CreateClient ();
			mClient.Connect( "127.0.0.1" , 7777 );

			// Server Event Callbacks
			mServer.OnConnection = ServerConnection;
			mServer.OnData = ServerData;
			mServer.OnDisconnection = ServerDisconnect;

			// Client Event Callbacks
			mClient.OnConnection = ClientConnection;
			mClient.OnData = ClientData;
			mClient.OnDisconnection = ClientDisconnect;

		}

		void Update() {
			NetManager.PollEvents();
		}

		public void ServerConnection( int connectionId , int channelId , byte[] buffer , int datasize ){
			Debug.Log ( "Server: Connection to " + connectionId.ToString () );
		}

		public void ServerData( int connectionId , int channelId , byte[] buffer , int datasize ){
			string msg = Encoding.ASCII.GetString(buffer, 0, datasize);
			Debug.Log ("Server: Received message from " + connectionId.ToString () + " : " + msg );
			var bytes = System.Text.Encoding.ASCII.GetBytes("Server says hello");
			mServer.BroadcastStream(bytes, bytes.Length, channelId );
		}

		public void ServerDisconnect( int connectionId , int channelId , byte[] buffer , int datasize ){
			Debug.Log ("Server: User disconnected from server! " + connectionId);
		}

		public void ClientConnection( int connectionId , int channelId , byte[] buffer , int datasize ){
			Debug.Log ( "Client: Connection to " + connectionId.ToString () );
			var bytes = Encoding.ASCII.GetBytes("TestMessage!");
			mClient.SendStream(bytes, bytes.Length, 0);
		}

		public void ClientData( int connectionId , int channelId , byte[] buffer , int datasize ){
			var msg = Encoding.ASCII.GetString(buffer, 0, datasize);
			Debug.Log ("Client: Data Received! " + msg);
			mClient.Disconnect();
		}

		public void ClientDisconnect( int connectionId , int channelId , byte[] buffer , int datasize ){
			Debug.Log ("Client: Disconnected from server! " + connectionId);
		}
	}
}



Notes

  • You need to call NetManager.PollEvents() every frame.
  • For clients, if you want a client to disconnect and remove itself, you may need to wait a frame after calling the Disconnect() method before NetManager.DestroyClient() or NetManager.Shutdown(). Otherwise the client may not send the disconnect message before it destroys itself.

unetllapiwrapper's People

Contributors

hyakugei avatar linadk avatar

Stargazers

Matt Sylvia avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.