Unity even photon server Getting Started

Source: Internet
Author: User

Photon is now a more useful game server. Currently on the Internet for Photon server to explain relatively little, recently also to photon do a preliminary understanding, do an extremely detailed introduction.

The first thing is to download photon.

Https://www.photonengine.com/en/OnPremise/Download This is the server download. (The function of uploading pictures crashes ...) )

This server is authorized, free of charge for 100 people and 30 days free, debugging with the words, 100 people are still very cost-effective.

Https://www.photonengine.com/en/OnPremise/Dashboard (must register account Oh ...) )

Here is a better tutorial http://download.csdn.net/download/a762923309/4936547 is free, download is good (CSDN)

If you can read the tutorial you can go to the top right corner.

Now get to the point. In the deployment of the server is configured for all servers, you will see a few launch version bin_win32,bin_win32_xp, according to their own system environment to choose.

My system is win10 selected is Bin_win64 inside there is a PhotonControl.exe is to run the server. Double-click to start it.

In the lower right corner of your system you will find a small circle, this is the server!

Right click on it you will find a photon instance: The following is a default is the server we want to use, the above tutorial is different here, but not much worse.

By the way, the downloaded permissions are placed in this bin folder, my is Bin_win64, finish the permissions remember to restart the server AH.

Let's write down the server code. A simple user Login

Photon in C # We use vs Write, I use VS2015

First we create a new C # class library We call MyServer, let's introduce 3 DLLs, in the photon Lib

ExitGamesLibs.dll

Photon.SocketServer.dll

PhotonHostRuntimeInterfaces.dll

Create a new C # class We call Mypeer, inherit peerbase, then rewrite the function, and don't forget the using

usingPhoton.socketserver;usingphotonhostruntimeinterfaces;namespacemyserver{usingMessage; usingSystem.Collections;  Public classmypeer:peerbase {Hashtable usertabel;  PublicMypeer (Irpcprotocol protocol,iphotonpeer photonpeer):Base(protocol, photonpeer) {Usertabel=NewHashtable (); Usertabel.add ("123","1234"); }        protected Override voidOnDisconnect (Disconnectreason Reasoncode,stringreasondetail) {            //issues to be dealt with when the connection is lost, such as releasing resources        }        protected Override voidonoperationrequest (operationrequest operationrequest, sendparameters sendparameters) {//get the client's end of the request to deal with            Switch(operationrequest.operationcode) { Case(byte) Opcodeenum.login:stringuname = (string) operationrequest.parameters[(byte) Opkeyenum.username]; stringPWD = (string) operationrequest.parameters[(byte) Opkeyenum.password]; if(Usertabel.containskey (uname) &&Usertabel[uname]. Equals (pwd) {sendoperationresponse (NewOperationresponse ((byte) Opcodeenum.loginsuccess,NULL),Newsendparameters ()); }                    Else{sendoperationresponse (NewOperationresponse ((byte) opcodeenum.loginfailed,NULL),Newsendparameters ()); }                     Break; }        }    }}

And then we build a C # class called MyApplication, we inherit ApplicationBase, and then rewrite it all, and I give the meaning of each function.

usingPhoton.socketserver;namespacemyserver{ Public classMyapplication:applicationbase {protected Overridepeerbase Createpeer (initrequest initrequest) {//establish the connection and pass it back to Photon Server            return NewMypeer (Initrequest.protocol, Initrequest.photonpeer); }        protected Override voidSetup () {//Initialize Gameserver        }        protected Override voidTearDown () {//Close Gameserver and release resources        }    }}

A message is also used to identify the state, and a new C # class called a message

namespacemyserver.message{enumOpcodeenum:byte{Login=1, Loginsuccess=2, loginfailed=3, Create= -, Join=255, Leave=254, RaiseEvent=253, SetProperties=252, GetProperties=251    }    enumOpkeyenum:byte{Roomid=251, UserName=252, PassWord=253    }}

And then an important step, in the VS solution, we right-click our MyServer (C # Class library name) to open the properties, select Generate, and change the output path in the output to bin\

Because photon reads the DLLs in the bin directory.

Then we'll build the server just fine ~ ~ ~

Then put our server MyServer in addition to the Bin folder can be deleted, and then put in photon in the Deploy folder, and then we configure the photon

Open the Bin directory in the Deploy directory and I'll open the Photonserver.config in Bin_win64 and open it with VS

It is recommended to read the comments in the Photonserver.config file, not English can be used in Youdao. Very helpful

We use UDP transmission mode, photon only one answer port is 5055, so the firewall does not seal this port and 843, is unity and flash a connection port so do not seal, the firewall will not open the fixed port see/HTTP Windows.microsoft.com/zh-cn/windows/open-port-windows-firewall#1tc=windows-7

Then we'll add a piece of code in <applications default= "Lite" > Below

<!--MyServer Application-<Application Name="MyServer"basedirectory="MyServer"Assembly="MyServer"Type="myserver.myapplication"Forceautorestart="true"Watchfiles="Dll;config"Excludefiles="Log4net.config"> </Application>

Then save it.

So we're done with the server side, now let's open start as application in default and open Logs See Server is running ... The surface server was established successfully.

And then the unity side.

We create a new project and then introduce a DLL directly to unity in the line Photon3Unity3D.dll also in Lib.

Let's build a C # Script called Hotonsocket, which also imports Photon3Unity3D.dll in the reference

usingUnityengine;usingExitGames.Client.Photon;usingSystem.Collections.Generic; Public classPhotonsocket:monobehaviour,iphotonpeerlistener {#regionSingle casePrivate StaticPhotonsocket _instance;  Public Staticphotonsocket Instance {Get{return_instance;} }    #endregion    Private stringaddress;//preferably in awake or start assignment, Unity small problem, easy to create value does not change, and best written in privatePrivate stringServer;//Ibid.PrivatePhotonpeer peer;  PublicClientState State; voidAwake () {_instance= This; Address="localhost:5055"; Server="MyServer"; State=Clientstate.disconnect; Peer=NewPhotonpeer ( This, CONNECTIONPROTOCOL.UDP); Peer.    Connect (address, Server); }     Public voidSendMessage (bytecode,dictionary<byte,Object>param) {Peer. Opcustom (Code, param,true); }    voidUpdate () {peer.    Service (); }     Public voidDebugreturn (DebugLevel level,stringmessage) {    }     Public voidOnEvent (EventData EventData) {} Public voidonoperationresponse (Operationresponse operationresponse) {Switch(operationresponse.operationcode) { Case(byte) OpCodeEnum.LoginSuccess:Debug.Log ("Login Success"); State=clientstate.loginsuccess;  Break;  Case(byte) OpCodeEnum.LoginFailed:Debug.Log ("Login Failed"); State=clientstate.loginfailed;  Break; }    }     Public voidonstatuschanged (StatusCode StatusCode) {Switch(statusCode) { CaseStatusCode.Connect:Debug.Log ("Connect");  Break;  CaseStatusCode.Disconnect:Debug.Log ("DisConnect");  Break; }    }     Public enumClientState:byte{DisConnect, Connect, loginsuccess, loginfailed}enumOpcodeenum:byte    {        //LoginLogin =1, Loginsuccess=2, loginfailed=3,    }}

So unity part also finished, you can come to test, appeared to connect the debug Surface link server success, appear loginsuccess OK.

All of the above is part of what we can find in the tutorials, and then let's say the address of the Unity section that is not in the tutorial.

If you want to be on the LAN network will find your native intranet IP, and then let address = native ip:5055 so OK,

If you want to network links there are two cases, one you are using routers, there are two ways, a DMZ host, a virtual server open.

The first kind of DMZ host, not recommended in this way, he will put your IP completely exposed to the external network, unsafe, address = External network ip:5055

The second kind of virtual server, this way is open part of the port, the higher-end router can set port-to-port, not high-end routers only specify the port, address = WAN Port ip:5055

Without the server, the next peanut shell software, he will give you a free domain name, and then hang on your extranet IP then address = Peanut Shell domain Name: 5055

Unity even photon server Getting Started

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.