Skip to content

Commit bd97a41

Browse files
committed
testing.
1 parent 702cd3e commit bd97a41

File tree

10 files changed

+156
-54
lines changed

10 files changed

+156
-54
lines changed

SampleProject/Assets/Editor/DummyServer.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@
1010
[InitializeOnLoad] public class DummyServer {
1111

1212
static DummyServer () {
13-
Debug.Log("initialized.");
13+
Debug.Log("ignore.");
14+
return;
1415

15-
Disquuun disquuun = null;
1616

1717
if (!EditorApplication.isPlaying && EditorApplication.isPlayingOrWillChangePlaymode) {
1818
// pass.
1919
} else {
2020
return;
2121
}
22+
23+
24+
Debug.Log("initializing.");
25+
Disquuun disquuun = null;
2226

2327
// set server handler.
2428
{
@@ -36,7 +40,7 @@ static DummyServer () {
3640
}
3741

3842
disquuun = new Disquuun(
39-
"127.0.0.1",
43+
"192.168.11.5",
4044
7711,
4145
1024,
4246
5,
Binary file not shown.

SampleProject/Assets/Samples/Connect/ConnectionSampleScript.cs

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,56 @@ webuSocket connection sample.
1515
public class ConnectionSampleScript : MonoBehaviour {
1616

1717
WebuSocket webSocket;
18-
18+
private string serverIP = "13.230.48.184";
19+
1920
bool opened = false;
2021

2122
public const string userId = "test";
2223
UdpClient udp;
23-
24+
IPEndPoint remoteEP = null;
25+
26+
private int udpReceiveCount;
27+
private bool achieved;
28+
29+
void OnGUI () {
30+
GUILayout.Label("udpReceiveCount:" + udpReceiveCount);
31+
GUILayout.Label("achieved:" + achieved);
32+
}
33+
2434
private void ThreadMethod () {
35+
36+
2537
while(true)
2638
{
27-
IPEndPoint remoteEP = null;
28-
byte[] data = udp.Receive(ref remoteEP);
29-
using (var sw = new System.IO.StreamWriter("received", true)) {
30-
sw.WriteLine(data.Length);
39+
try {
40+
Debug.Log("start waiting.");
41+
byte[] data = udp.Receive(ref remoteEP);
42+
remoteEP = null;
43+
string text = Encoding.ASCII.GetString(data);
44+
udpReceiveCount++;
45+
46+
if (text.Contains(":")) {
47+
// ここまでの受信はできる。
48+
49+
var ipAndPort = text.Split(':');
50+
var currentReceivedIp = ipAndPort[0];// サーバが返してきたクライアントのglobal ip
51+
var currentReceivedPort = ipAndPort[1];// サーバが返してきたクライアントのglobal port
52+
53+
54+
// var currentReceivedIp = ((IPEndPoint)(udp.Client.LocalEndPoint)).Address.ToString();// このipはローカルipなのでグローバルではない、LAN内でのみ使える。(たまたま一致する。)
55+
// var currentReceivedPort = port.ToString();// 接続時に使ったポート。
56+
Connect(currentReceivedIp, currentReceivedPort);
57+
continue;
58+
}
59+
60+
achieved = true;
61+
} catch (Exception e) {
62+
Debug.LogError("e:" + e);
63+
Thread.CurrentThread.Abort();
3164
}
32-
string text = Encoding.ASCII.GetString(data);
33-
Debug.LogError(text);
3465
}
35-
}
36-
public static string GetLocalIPAddress () {
37-
var host = Dns.GetHostEntry(Dns.GetHostName());
38-
foreach (var ip in host.AddressList) {
39-
if (ip.AddressFamily == AddressFamily.InterNetwork) {
40-
return ip.ToString();
41-
}
42-
Debug.Log("ip.ToString():" + ip.ToString());
43-
}
44-
throw new Exception("No network adapters with an IPv4 address in the system!");
45-
}
66+
}
67+
private int port;
4668

4769
void Start () {
4870
IPAddress localIP;
@@ -53,19 +75,49 @@ void Start () {
5375
Debug.Log("localIP:" + localIP.ToString());
5476
}
5577

78+
79+
// // サーバへとudpを送付する
80+
// if (false) {
81+
// using (var udpSender = new UdpClient()) {
82+
// // udpSender.Connect(IPAddress.Parse("127.0.0.1"), 7777);
83+
// // public int Send(byte[] dgram, int bytes, IPEndPoint endPoint);
84+
// var ep = new IPEndPoint(IPAddress.Parse(serverIP), 7777);
85+
// // var ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7777);
86+
// udpSender.Send(new byte[]{1,2,3,4}, 4, ep);
87+
// port = ((IPEndPoint)udpSender.Client.LocalEndPoint).Port;
88+
// }
89+
// Debug.Log("send udp. port:" + port);
90+
// }
91+
5692
// クライアント側は自分が接続しているエンドポイントのipがわかればそれでいい。そこにudpが降ってくるのを待つ。
93+
// 送信と受信を一手にできそう。、、もしかしてこれがいけないのか?
5794
if (true) {
58-
udp = new UdpClient(new IPEndPoint(new IPAddress(localIP.GetAddressBytes()), 7777));
59-
udp.Client.ReceiveTimeout = 1000;
95+
udp = new UdpClient(new IPEndPoint(new IPAddress(localIP.GetAddressBytes()), 8080));
96+
var ep = new IPEndPoint(IPAddress.Parse(serverIP), 8080);
97+
var bytes = Encoding.UTF8.GetBytes("hello!");
98+
99+
udp.Send(bytes, bytes.Length, ep);
100+
101+
port = ((IPEndPoint)udp.Client.LocalEndPoint).Port;
102+
Debug.Log("udp sending port:" + port);
103+
// udp.Client.ReceiveTimeout = 1000;
60104

61105
var thread = new Thread(new ThreadStart(ThreadMethod));
62106
thread.Start();
63107
}
108+
}
64109

110+
private void Connect (string udpIp, string udpPort) {
111+
Debug.Log("udp receiving udpIp:" + udpIp + " port:" + udpPort);
112+
// var udp2 = new UdpClient();
113+
// var ep = new IPEndPoint(IPAddress.Parse(serverIP), Convert.ToInt32(udpPort));
114+
// udp2.Send(new byte[]{1,2,3,4}, 4, ep);
115+
65116
webSocket = new WebuSocket(
66117
// url.
67118
// "wss://echo.websocket.org:443/",
68-
"ws://127.0.0.1:8080/sample_disque_client",
119+
// "ws://127.0.0.1:8080/sample_disque_client",
120+
"ws://" + serverIP + ":8080/sample_disque_client",
69121

70122
// buffer size.
71123
1024,
@@ -113,7 +165,8 @@ these data array will be destroyed soon after leaving this block.
113165
new Dictionary<string, string>{
114166
// set WebSocket connection header parameters here!
115167
{"id", userId},
116-
{"debugport", localIP.ToString()}
168+
// {"debugaddr", udpIp},
169+
{"debugport", udpPort}
117170
}
118171
);
119172
}

SampleProject/received

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
PROJECT_PATH=$(pwd)
22

3-
NGINX_VERSION=1.11.9
3+
NGINX_VERSION=1.13.6
44
NGX_DEVEL_KIT="dependencies/ngx_devel_kit-0.3.0"
5-
LUA_NGX_MOD="dependencies/lua-nginx-module-0.10.7"
5+
LUA_NGX_MOD="dependencies/lua-nginx-module-0.10.11"
6+
LUA_NJS_MOD="dependencies/njs-0.1.14/nginx"
67

78
export LUAJIT_LIB=/usr/local/lib
89
export LUAJIT_INC=/usr/local/include/luajit-2.1
910

1011
# make & install nginx to PROJECT_PATH/NGINX_VERSION
1112
./configure \
13+
--with-stream \
1214
--with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" \
1315
--prefix=$PROJECT_PATH/$NGINX_VERSION \
1416
--add-module=$NGX_DEVEL_KIT \
15-
--add-module=$LUA_NGX_MOD
17+
--add-module=$LUA_NGX_MOD \
18+
--add-module=$LUA_NJS_MOD
19+
1620

1721
make -j2
1822
make install
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function foo(s) {
2+
s.log("here comes!");
3+
return "oh,, " + s.remoteAddress + ":" + s.remotePort;
4+
}

nginx-luajit-ws/DockerResources/lua/sample_disque_client.lua

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,41 @@ if not the_id then
1818
the_id = "_empty_"
1919
end
2020

21+
local debug_addr = ngx.req.get_headers()["debugaddr"]
22+
if not debug_addr then
23+
-- docker内だとこのipは172で固定されてるような気がする。
24+
debug_addr = ngx.var.remote_addr
25+
end
26+
27+
2128
local debug_port = ngx.req.get_headers()["debugport"]
2229
if not debug_port then
2330
-- debugport指定がない場合、通信元をターゲットとしてudp通信を行う。 公式の通信では禁止すべき。
24-
debug_port = ngx.var.remote_addr
31+
debug_port = ngx.var.remote_port
2532
end
2633

27-
local udpsock = ngx.socket.udp()
34+
--local udpsock = ngx.socket.udp()
35+
--ok, err = udpsock:setpeername(debug_addr, ngx.var.remote_port)
36+
--ok, err = udpsock:setsockname(debug_addr, ngx.var.remote_port)
37+
38+
39+
-- kDDI回線ではこの条件で通過できた。
40+
do
41+
local udpsock = ngx.socket.udp()
42+
ok, err = udpsock:setpeername(debug_addr, debug_port)
43+
44+
ngx.log(ngx.ERR, "udpsock ok:", ok, " err:", err, " debug_addr:", debug_addr, " debug_port:", debug_port, " ngx.var.remote_addr:", ngx.var.remote_addr, " ngx.var.remote_port:", ngx.var.remote_port)
45+
-- sendtoはない。
46+
local ok, err = udpsock:send("testdata7777-2")
47+
ngx.log(ngx.ERR, "udp send ok:", ok, " err:", err)
48+
--udpsock.close()
49+
end
2850

29-
ok, err = udpsock:setpeername(debug_port, 7777)
30-
ngx.log(ngx.ERR, "udpsock ok:", ok, " err:", err, " debug_port:", debug_port)
3151

3252

53+
--local data, err = udpsock:receive()
54+
--ngx.log(ngx.ERR, "udp data:", data, " err:", err)
55+
3356

3457
ip = "127.0.0.1"-- localhost.
3558
port = 7711
@@ -247,9 +270,4 @@ connectWebSocket()
247270

248271
-- それが解消したらできそうかな?できそうだな。
249272
-- パラメータを保持させて、か、、まあ親のインスタンスのパラメータに触れるのはしんどいんで、やっぱりluaだと厳しいねっていう話になるのがいい気がする。
250-
-- 本当にあると嬉しいのは、TCP以外が喋れる、フロントになれるメッセージキューか。まあErlangにはあるんだけどな。
251-
252-
253-
254-
255-
273+
-- 本当にあると嬉しいのは、TCP以外が喋れる、フロントになれるメッセージキューか。まあErlangにはあるんだけどな。

nginx-luajit-ws/DockerResources/nginx.conf

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# load_module modules/ngx_stream_js_module.so;
2+
13
#user nobody;
24
worker_processes auto;
35

@@ -41,7 +43,7 @@ http {
4143
# lua_package_cpath ';;$prefix/lua/shared/?.so;';
4244

4345
server {
44-
listen 80;
46+
listen 8080;
4547
server_name localhost;
4648

4749
access_log logs/host.access.log;
@@ -56,5 +58,20 @@ http {
5658
content_by_lua_file lua/sample_disque_client.lua;
5759
}
5860
}
61+
}
62+
63+
stream {
64+
# js_include "/nginx-1.13.6/1.13.6/js/stream.js";
65+
# js_set $foo foo;
66+
67+
log_format main '$remote_addr:$remote_port';
68+
access_log logs/host.access.log main;
5969

70+
server {
71+
listen 8080 udp;
72+
73+
# addrとportを返す
74+
return $remote_addr:$remote_port;
75+
}
76+
# エラーとアクセスが同時に出るの間違ってる気がするな〜〜。とはいえ動いてるのがわかった。
6077
}

nginx-luajit-ws/rebuild.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
docker rm nginx_luajit -f
1+
docker rm -f nginx_luajit
22
docker build -f ubuntu.dockerfile -t nginx-luajit-ubuntu .
3-
docker run -ti -d --name nginx_luajit -p 8080:80 -p 7777:7777 -p 7711:7711 -v $(pwd)/logs:/nginx-1.11.9/1.11.9/logs nginx-luajit-ubuntu
3+
docker run -ti -d --name nginx_luajit -p 8080:80 -p 7777:7777/udp -v $(pwd)/logs:/nginx-1.13.6/1.13.6/logs nginx-luajit-ubuntu

nginx-luajit-ws/ubuntu.dockerfile

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
FROM ubuntu
22

3-
ENV NGINX_VERSION 1.11.9
3+
ENV NGINX_VERSION 1.13.6
44
ENV LUAJIT_VERSION 2.1.0-beta2
55
ENV NGINX_DEVEL_KIT_VERSION v0.3.0
6-
ENV NGINX_LUAJIT_VERSION v0.10.7
6+
ENV NGINX_LUAJIT_VERSION v0.10.11
7+
ENV NJS_VERSION 0.1.14
8+
79

810
# ready tools.
911
RUN apt-get update && apt-get install -y \
@@ -22,10 +24,13 @@ RUN wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz && tar -xzvf ngin
2224
# add luajit module.
2325
RUN wget http://luajit.org/download/LuaJIT-$LUAJIT_VERSION.tar.gz && tar -xzvf LuaJIT-$LUAJIT_VERSION.tar.gz && rm LuaJIT-$LUAJIT_VERSION.tar.gz && cd LuaJIT-$LUAJIT_VERSION/ && make && make install
2426

25-
# add nginx tools and lua module.
27+
28+
# add nginx tools, lua module and njs module.
2629
RUN mkdir nginx-$NGINX_VERSION/dependencies && cd nginx-$NGINX_VERSION/dependencies && \
2730
wget https://github.com/simpl/ngx_devel_kit/archive/$NGINX_DEVEL_KIT_VERSION.zip && unzip $NGINX_DEVEL_KIT_VERSION.zip && rm $NGINX_DEVEL_KIT_VERSION.zip && \
28-
wget https://github.com/openresty/lua-nginx-module/archive/$NGINX_LUAJIT_VERSION.zip && unzip $NGINX_LUAJIT_VERSION.zip && rm $NGINX_LUAJIT_VERSION.zip
31+
wget https://github.com/openresty/lua-nginx-module/archive/$NGINX_LUAJIT_VERSION.zip && unzip $NGINX_LUAJIT_VERSION.zip && rm $NGINX_LUAJIT_VERSION.zip && \
32+
wget https://github.com/nginx/njs/archive/$NJS_VERSION.zip && unzip $NJS_VERSION.zip && rm $NJS_VERSION.zip
33+
2934

3035
# add shell.
3136
COPY ./DockerResources/build.sh nginx-$NGINX_VERSION/build.sh
@@ -41,9 +46,13 @@ RUN wget https://github.com/antirez/disque/archive/master.zip && unzip master.zi
4146
RUN mkdir nginx-$NGINX_VERSION/$NGINX_VERSION/lua && ls -l
4247
COPY ./DockerResources/lua nginx-$NGINX_VERSION/$NGINX_VERSION/lua
4348

49+
50+
# add js sources.
51+
RUN mkdir nginx-$NGINX_VERSION/$NGINX_VERSION/js && ls -l
52+
COPY ./DockerResources/js nginx-$NGINX_VERSION/$NGINX_VERSION/js
53+
4454
# overwrite nginx conf.
4555
COPY ./DockerResources/nginx.conf nginx-$NGINX_VERSION/$NGINX_VERSION/conf/
4656

47-
4857
# run nginx & disque-server.
4958
ENTRYPOINT /nginx-$NGINX_VERSION/$NGINX_VERSION/sbin/nginx && /disque-master/src/disque-server

0 commit comments

Comments
 (0)