在阿里云ECS上搭建Skynet服务器与Unity通信
在阿里云ECS上搭建Skynet服務器與Unity通信
- 創建阿里云ECS實例
- Skynet搭建
- 服務端代碼部分
- 客戶端部分(Unity)
創建阿里云ECS實例
這部分的話按照阿里云流程去做就可以了。也可以使用谷歌云或者其他的云VPS。
Skynet搭建
Ubuntu下的環境搭建(其他系統下并未嘗試):
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
服務端代碼部分
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"-- 啟動服務(啟動函數) skynet.start(function()-- 啟動函數里調用Skynet API開發各種服務print("======Server start=======")skynet.newservice("socket")skynet.exit() end)socket.lua 文件,此處特別注意監聽的ip必須是阿里云的私有ip,端口號需要在阿里云設置中進行開啟入規則端口
local skynet = require "skynet" local socket = require "skynet.socket"-- 讀取客戶端數據, 并輸出 local function echo(id)-- 每當 accept 函數獲得一個新的 socket id 后,并不會立即收到這個 socket 上的數據。這是因為,我們有時會希望把這個 socket 的操作>權轉讓給別的服務去處理。-- 任何一個服務只有在調用 socket.start(id) 之后,才可以收到這個 socket 上的數據。socket.start(id)while true do -- 讀取客戶端發過來的數據 local str = socket.read(id)if str then -- 直接打印接收到的數據print(str)elsesocket.close(id)returnendend end skynet.start(function() print("==========Socket1 Start=========")-- 監聽一個端口,返回一個 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)-- 接收到客戶端連接或發送消息()print("connect from " .. addr .. " " .. id)-- 處理接收到的消息echo(id)end)--可以為自己注冊一個別名。(別名必須在 32 個字符以內)-- skynet.register "SOCKET" end)這里很重要的一點是阿里云的ECS實例上的telnet是連接不通自己的回環地址127.0.0.1的
必須安裝telnet的配置才能夠訪問。也就是說沒有安裝telnet外網也無法通過socket連接至阿里云上的服務器。
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
出現則可以外網成功連接了。
之后前往skynet根目錄創建bash文件run.sh
touch run.sh
#!/bin/bash./skynet ./myServer/config并且在根目錄
sh run.sh
開啟服務器監聽
客戶端部分(Unity)
創建一個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("連接服務器成功");receiveT = new Thread(ReceiveMsg);receiveT.Start();}catch (System.Exception ex){Debug.Log("連接服務器失敗!");Debug.Log(ex.Message);}}private void ReceiveMsg(){while (true){if (clientSocket.Connected == false){Debug.Log("與服務器斷開了連接");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();//關閉連接}if (receiveT != null){receiveT.Interrupt();receiveT.Abort();}}catch (Exception ex){Debug.Log(ex.Message);}} }掛載到隨便某個物體上,運行測試,這邊注意連接的ip是阿里云公網ip
測試成功截圖:
客戶端
服務端
總結
以上是生活随笔為你收集整理的在阿里云ECS上搭建Skynet服务器与Unity通信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: h5 实现微信支付以及易宝银行卡支付
- 下一篇: 短信或者邮件链接打开 APP(URL S