http://www.csdn.net/article/2015-10-26/2826038-Erlang
http://blog.csdn.net/swordfishx82/article/details/45227813
http://blog.csdn.net/swordfishx82/article/details/45241887
從網(wǎng)上下載erlang64位,python2.7安裝到電腦上,然后設(shè)置環(huán)境變量
,然后在網(wǎng)上下載傾國傾城的客戶端和服務(wù)器端,進(jìn)入服務(wù)器端文件夾,點擊startup.bat


修改客戶端連接的配置文件
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plist version="1.0">
- <dict>
- <key>1</key>
- <dict>
- <key>idx</key>
- <string>1</string>
- <key>name</key>
- <string>一區(qū)</string>
- <key>servers</key>
- <dict>
- <key>100</key>
- <dict>
- <key>idx</key>
- <string>100</string>
- <key>name</key>
- <string>簡雨測試服</string>
- <key>ip</key>
- <string>127.0.0.1</string>
- </dict>
- <key>300</key>
- <dict>
- <key>idx</key>
- <string>300</string>
- <key>name</key>
- <string>測試服1</string>
- <key>ip</key>
- <string>127.0.0.1</string>
- </dict>
- <key>400</key>
- <dict>
- <key>idx</key>
- <string>400</string>
- <key>name</key>
- <string>測試服2</string>
- <key>ip</key>
- <string>127.0.0.1</string>
- </dict>
- </dict>
- </dict>
- <key>2</key>
- <dict>
- <key>idx</key>
- <string>2</string>
- <key>name</key>
- <string>二區(qū)</string>
- <key>servers</key>
- <dict>
- <key>200</key>
- <dict>
- <key>idx</key>
- <string>200</string>
- <key>name</key>
- <string>9秒測試服</string>
- <key>ip</key>
- <string>127.0.0.1</string>
- </dict>
- <key>500</key>
- <dict>
- <key>idx</key>
- <string>500</string>
- <key>name</key>
- <string>測試服3</string>
- <key>ip</key>
- <string>127.0.0.1</string>
- </dict>
- </dict>
- </dict>
- </dict>
- </plist>
有vs2010打開客戶端,
跟正常的cocos2dx編譯方法沒有區(qū)別,





這個游戲的架設(shè)方法跟kbe架設(shè)難度沒有可比性,不過erlang在國內(nèi)受歡迎程度可不是一般的,自從神仙道橫行國內(nèi)頁游市場之后,越來越多的公司注意erlang服務(wù)器了,傾國傾城這個游戲雖然客戶端是純c++,不能像lua一樣進(jìn)行熱更新,不過貴在寫的很完整,值得一看,另外服務(wù)器端是一個完整網(wǎng)友的腳本系統(tǒng),對學(xué)習(xí)erlang有相當(dāng)大的幫助,好了就到這。
這里先占個位置,昨晚的文章不知怎么有更出來了,所以在后面補(bǔ)一段erlang的基本語法吧
一、Erlang語法:變量、模式匹配
--------------------------------------
1.elr命令:工具欄toolbar:start().
2.%。。。注釋
3.變量首字母大寫,單一賦值
4.模式匹配
5.原子:hello, 'an atom with'
6.元組tuple:元組嵌套{person,{a,1},{b,2}}
提取元組字段值Point = {point, 10, 45} {point, X, Y} = Point
7.列表list: 定義列表ThingsToBuy1 = [{oranges,4}, {newspaper,1}|{ThingsToBuy}]
提取元素[Buy1|ThingsToBuy2] = ThingsToBuy1.
8.字符串:"hello"
9.q()退出
10.f()釋放變量
二、Erlang語法:函數(shù)(面向函數(shù)編程:函數(shù)可以作為參數(shù),也可以作為返回值,可以使用列表解析、斷言、case/if、二進(jìn)制、比特位、進(jìn)制、ASCII碼)
--------------------------------------
1.函數(shù)
編譯:c(geometry)
運(yùn)行:geometry:area({rectangle, 10, 5}).
2.匿名函數(shù):Double = fun(X) -> 2*X end.
Double(2).
Hypot = fun(X, Y) -> math:sqrt(X*X+Y*Y) end.
TempConvert = fun({c,C}) -> {f, 32+C*9/5};
({f,F}) -> {c, (F-32)*5/9}
end.
3.fun作為函數(shù)參數(shù)
映射:lists:map(Double, [1,2,3,4]).
返回[2,4,6,8].
過濾:lists:filter(Even, [1,2,3,4]).
Even = fun(X) -> (X rem 2) =:= 0 end.
4.返回fun的函數(shù)
Fruit = [apple,pear,orange].
MakeTest = fun(L) -> (fun(X) -> lists:member(X,L) end) end.
IsFruit = MakeTest(Fruit).
即:IsFruit = fun(X) -> lists:member(X,[apple,pear,orange]) end.
5.循環(huán)
for(Max,Max,F) -> [F(Max)];
for(I,Max,F) -> [F(I)|for(I+1,Max,F)]
6.列表解析
[2*X || X <- L].
定義:Buy = [{oranges,4},{newspaper,1},{apples,10},{pears,6},{milk,3}].
乘積:[shop:cost(A)*B || {A,B} <- Buy]
求和:libs:sum([shop:cost(A)*B || {A,B} <- Buy]).
(1)快速排序:L=[12,6,2,13,2,8,9,10]. qsort(L).
(2)畢達(dá)哥拉斯三元組:libs:pythag(16).
(3)變位詞:libs:perms("123").
["123","132","213","231","312","321"]
7.算術(shù)表達(dá)式
8.斷言:
max(X,Y) when X > Y -> X;
max(X,Y) -> Y.
斷言函數(shù):
f(X,Y) when is_integer(X), X > Y, Y < 6 -> X;
9.記錄:record
10.case表達(dá)式:
filter(P, [P|T]) ->
case P(H) of
true -> [H|filter(P,T}];
false ->filter(P,T)
end;
filter(P, []) -> [].
11.if表達(dá)式:
12.內(nèi)建函數(shù)BIF
元組轉(zhuǎn)換為列表:tuple_to_list({12,cat,"Hello"}).
系統(tǒng)時間:time()
13.二進(jìn)制數(shù)據(jù):<<5,10,20>>,<<"hello">>
操縱:list_to_binary()
term_to_binary()
binary_to_term()
size()
14.比特語法:<>
file:read_file("")
15.動態(tài)調(diào)用:apply(libs, sum, [1,2,3,4]).
16.模塊定義:-module(modname).
引入:-import(modname, [fun/1]). 可以不寫模塊名直接引用函數(shù)名
導(dǎo)出:-export([name1/1, name2/2]).
編譯屬性:-compile(options).
模塊版本:-vsn(Version).
用戶定義屬性:-SomeTag(Value).
輸出屬性:attr:module_info().
attr:module_info(attributes).
引用函數(shù):fun Mod:RemoteFunc/Arity
包含文件:-include(Filename).
-include_lib(Name).
列表操作符:[1,2,3]++[4,5,6]
[1,2,3]--[2,3]
進(jìn)制:K#123
$語法:$C 表示ASCII
字典:erase(), put(x, 20),get(x), erase(x)
短路表達(dá)式:Epr1 orelse Epr2. Epr1 andalse Epr2.
三、Erlang語法:編譯運(yùn)行
--------------------------------------
1.退出:halt().導(dǎo)致數(shù)據(jù)庫要恢復(fù) q().安全退出,等價于init:stop().
2.取得加載路徑:code:get_path().
取得主目錄:init:get_argument(home).
3.外部運(yùn)行:D:/erl>erl.exe -noshell -s hello start -s init stop
4.幫助:help().
四、Erlang語法:并發(fā)(進(jìn)程類似于人:通過消息進(jìn)行溝通,也可以廣播;沒有共享內(nèi)存,因此不需要鎖;某一個死掉,會通知鏈接進(jìn)程)
--------------------------------------
1.創(chuàng)建進(jìn)程:Pid = spawn(Fun)
spawn(Mod, FuncName, Args).
2.發(fā)送消息:Pid!Message
群發(fā)消息:Pid1!Pid2!Pid3...!Pidn!Message
接收消息:receive ... end.
例如:Pid = spawn(fun area_server0:loop/0).
Pid!{rectangle, 6, 10}.
3.自身ID:self().
Pid!{self(), Message}.
例如:Pid = spawn(fun area_server1:loop/0).
area_server1:rpc(Pid, {rectangle, 6, 10}).
4.設(shè)置進(jìn)程數(shù):D:/erl>erl +P 500000
測試啟動時間:process:max(400000).
5.注冊進(jìn)程:register(AnAtom, Pid). 注冊進(jìn)程別名
unregister(AnAtom). 溢出注冊進(jìn)程
whereis(AnAtom) -> Pid | undefined 判斷是否注冊
registered() -> [AnAtom::atom()] 取得所有注冊進(jìn)程
例如注冊時鐘:clock:start(5000, fun() -> io:format("TICK ~p~n", [erlang:now()]) end).
停止時鐘:clock:stop().
6.并發(fā)錯誤:鏈接進(jìn)程
五、Erlang語法:分布式
--------------------------------------
1.單節(jié)點測試
啟動服務(wù)器:kvs.start().
存儲: kvs:store({location, joe}, "Stockholm").
kvs:store(weather, raining).
查找: kvs:lookup(weather).
kvs:lookup({location, joe}).
2.雙節(jié)點測試
啟動服務(wù)器節(jié)點:
D:/erl>erl -sname gandalf
Eshell V5.7 (abort with ^G)
(gandalf@zhongbingliu)1> kvs:start().
true
(gandalf@zhongbingliu)2>
調(diào)用者節(jié)點:
D:/erl>erl -sname bilbo
Eshell V5.7 (abort with ^G)
(bilbo@zhongbingliu)1> rpc:call(gandalf@zhongbingliu, kvs, store, [weather, fine]).
true
(bilbo@zhongbingliu)2> rpc:call(gandalf@zhongbingliu, kvs, lookup, [weather]).
{ok,fine}
3.客戶機(jī)和服務(wù)器位于同一局域網(wǎng)的不同機(jī)器上
4.客戶機(jī)和服務(wù)器位于因特網(wǎng)的不同機(jī)器上:確保4396端口通信正常,epmd會使用這個端口
六、Erlang語法:文件
--------------------------------------
1.讀取文件:file:consult("data1.data").
2.讀取一項:{ok, S} = file:open("data1.data", read). //打開
io:read(S, ''). //讀取一項
io:get_line(S, ''). //讀取一行
file:close(S). //關(guān)閉
3.查找代碼庫位置:code:which(file).
d:/erl5.7/lib/kernel-2.13/ebin/file.beam
4.讀取二進(jìn)制數(shù)據(jù):
file:read_file("data1.data"). //全部讀到內(nèi)存
{ok, S} = file:open("data1.data", [read,binary,raw]). //打開
file:pread(S, 22, 46). //隨機(jī)讀取
5.查找文件:lib_find:files
6.寫入文件:{ok, S} = file:open("data2.data", write). //打開
io:format(S, "~s~n", ["Hello readers"]). //寫入
file:close(S). //關(guān)閉
7.隨機(jī)寫入:
{ok, S} = file:open("data3.data", [raw,write,binary]). //打開
file:pwrite(S, 10, <<"new">>). //隨機(jī)寫入
8.目錄操作:file:list_dir("/"). //文件列表
file:make_dir("abc"). //創(chuàng)建目錄
file:del_dir("abc"). //刪除目錄
9.文件屬性:file:read_file_info("data1.data").
10.文件操作:file:copy("data1,data", "/"). //拷貝
file:delete("data1"). //刪除
七、Erlang語法:套接字
--------------------------------------
1.Socket連接www:socket:nano_get_url().
2.啟動服務(wù)器:server:start_nano_server().
3.編寫客戶端:client:nano_client_eval("list_to_tuple([2+3*4,10+20])").
主動型:非阻塞 服務(wù)器接收消息的速度必須快于客戶端發(fā)送的速度,否則服務(wù)器會因為消息緩沖區(qū)塞滿被消息淹沒
被動型:阻塞
混合型:半阻塞
八、Erlang語法:數(shù)據(jù)庫
--------------------------------------
1.ETS是內(nèi)存存儲,速度快
2.DETS是磁盤存儲,可備份
3.創(chuàng)建表:ets:new
dets:open_file
4.插入:insert(table, X)
5.查找:lookup(table, Key)
6.釋放:dets:close(tableid) etd:delete(tableid)
7.Mnesia數(shù)據(jù)庫:
創(chuàng)建表:-record...
選取所有數(shù)據(jù):do(qlc())...
選取部分列:
按條件選取:
關(guān)連查詢:
增加數(shù)據(jù):mnesia:write()
刪除數(shù)據(jù):mnesia:delete()
事務(wù)管理:mnesia:transaction(F)
取消事務(wù):mnesia:abort()
啟動表查看器:tv:start().
九、Erlang語法:OTP
--------------------------------------
1.支持事務(wù):使用異常捕捉進(jìn)行回滾
2.支持熱代碼替換
3.錯誤日志
4.警報管理
5.應(yīng)用程序監(jiān)視器:appmon:start().
十、Erlang語法:JInterface
--------------------------------------
OptNode 節(jié)點,監(jiān)聽端口 能夠啟動多個服務(wù)節(jié)點,如gurka@sallad.com
Epmd
OptMbox 默認(rèn)啟動一個郵箱 收發(fā)郵件
Link 監(jiān)控遠(yuǎn)程是否斷掉
posted on 2017-01-10 22:55
思月行云 閱讀(436)
評論(0) 編輯 收藏 引用 所屬分類:
Erlang