在阿里云ECS上搭建Skynet服务器与Unity通信
在阿里云ECS上搭建Skynet服務(wù)器與Unity通信
- 創(chuàng)建阿里云ECS實(shí)例
- Skynet搭建
- 服務(wù)端代碼部分
- 客戶端部分(Unity)
創(chuàng)建阿里云ECS實(shí)例
這部分的話按照阿里云流程去做就可以了。也可以使用谷歌云或者其他的云VPS。
Skynet搭建
Ubuntu下的環(huán)境搭建(其他系統(tǒng)下并未嘗試):
sudo apt-get update
apt-get install git
git clone https://github.com/cloudwu/skynet.git
apt-get install libreadline-dev autoconf
cd skynet
make linux
服務(wù)端代碼部分
mkdir myServer
touch config
touch main.lua
touch socket.lua
config文件
root = "./" thread = 8 logger = nil harbor = 1 address = "127.0.0.1:2526" master = "127.0.0.1:2013" start = "main" -- main script bootstrap = "snlua bootstrap" -- The service for bootstrap standalone = "0.0.0.0:2013" luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."myServer/?.lua" lualoader = "lualib/loader.lua" snax = root.."examples/?.lua;"..root.."test/?.lua" cpath = root.."cservice/?.so"main.lua文件
local skynet = require "skynet"-- 啟動(dòng)服務(wù)(啟動(dòng)函數(shù)) skynet.start(function()-- 啟動(dòng)函數(shù)里調(diào)用Skynet API開(kāi)發(fā)各種服務(wù)print("======Server start=======")skynet.newservice("socket")skynet.exit() end)socket.lua 文件,此處特別注意監(jiān)聽(tīng)的ip必須是阿里云的私有ip,端口號(hào)需要在阿里云設(shè)置中進(jìn)行開(kāi)啟入規(guī)則端口
local skynet = require "skynet" local socket = require "skynet.socket"-- 讀取客戶端數(shù)據(jù), 并輸出 local function echo(id)-- 每當(dāng) accept 函數(shù)獲得一個(gè)新的 socket id 后,并不會(huì)立即收到這個(gè) socket 上的數(shù)據(jù)。這是因?yàn)?#xff0c;我們有時(shí)會(huì)希望把這個(gè) socket 的操作>權(quán)轉(zhuǎn)讓給別的服務(wù)去處理。-- 任何一個(gè)服務(wù)只有在調(diào)用 socket.start(id) 之后,才可以收到這個(gè) socket 上的數(shù)據(jù)。socket.start(id)while true do -- 讀取客戶端發(fā)過(guò)來(lái)的數(shù)據(jù) local str = socket.read(id)if str then -- 直接打印接收到的數(shù)據(jù)print(str)elsesocket.close(id)returnendend end skynet.start(function() print("==========Socket1 Start=========")-- 監(jiān)聽(tīng)一個(gè)端口,返回一個(gè) id ,供 start 使用。local id = socket.listen("xxx.xx.xx.xxx", xxxx)print("Listen socket :", "xxx.xx.xx.xxx", xxxx)socket.start(id , function(id, addr)-- 接收到客戶端連接或發(fā)送消息()print("connect from " .. addr .. " " .. id)-- 處理接收到的消息echo(id)end)--可以為自己注冊(cè)一個(gè)別名。(別名必須在 32 個(gè)字符以內(nèi))-- skynet.register "SOCKET" end)這里很重要的一點(diǎn)是阿里云的ECS實(shí)例上的telnet是連接不通自己的回環(huán)地址127.0.0.1的
必須安裝telnet的配置才能夠訪問(wèn)。也就是說(shuō)沒(méi)有安裝telnet外網(wǎng)也無(wú)法通過(guò)socket連接至阿里云上的服務(wù)器。
sudo apt-get install xinetd telnetd
vim /etc/inetd.conf
vim /etc/xinetd.conf
Simple configuration file for xinetd # # Some defaults, and include /etc/xinetd.d/ defaults { # Please note that you need a log_type line to be able to use log_on_success # and log_on_failure. The default is the following : # log_type = SYSLOG daemon info instances = 60 log_type = SYSLOG authpriv log_on_success = HOST PID log_on_failure = HOST cps = 25 30 }sudo /etc/init.d/xinetd restart
最后嘗試
telnet 127.0.0.1
出現(xiàn)則可以外網(wǎng)成功連接了。
之后前往skynet根目錄創(chuàng)建bash文件run.sh
touch run.sh
#!/bin/bash./skynet ./myServer/config并且在根目錄
sh run.sh
開(kāi)啟服務(wù)器監(jiān)聽(tīng)
客戶端部分(Unity)
創(chuàng)建一個(gè)C#腳本
using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using UnityEngine;public class NetWorkScript : MonoBehaviour {private byte[] data = new byte[1024];private Socket clientSocket;private Thread receiveT;void Start (){ConnectToServer();}void ConnectToServer(){try{clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);clientSocket.Connect("xxx.xx.xx.xxx", xxxx);SendMes("hello world!");Debug.Log("連接服務(wù)器成功");receiveT = new Thread(ReceiveMsg);receiveT.Start();}catch (System.Exception ex){Debug.Log("連接服務(wù)器失敗!");Debug.Log(ex.Message);}}private void ReceiveMsg(){while (true){if (clientSocket.Connected == false){Debug.Log("與服務(wù)器斷開(kāi)了連接");break;}int lenght = 0;lenght = clientSocket.Receive(data);string str = Encoding.UTF8.GetString(data, 0, data.Length);Debug.Log(str);}}void SendMes(string ms){byte[] data = new byte[1024];data = Encoding.UTF8.GetBytes(ms);clientSocket.Send(data);}void OnDestroy(){try{if (clientSocket != null){clientSocket.Shutdown(SocketShutdown.Both);clientSocket.Close();//關(guān)閉連接}if (receiveT != null){receiveT.Interrupt();receiveT.Abort();}}catch (Exception ex){Debug.Log(ex.Message);}} }掛載到隨便某個(gè)物體上,運(yùn)行測(cè)試,這邊注意連接的ip是阿里云公網(wǎng)ip
測(cè)試成功截圖:
客戶端
服務(wù)端
總結(jié)
以上是生活随笔為你收集整理的在阿里云ECS上搭建Skynet服务器与Unity通信的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: h5 实现微信支付以及易宝银行卡支付
- 下一篇: 短信或者邮件链接打开 APP(URL S