Skip to content

Commit 67557e6

Browse files
committed
added test.js
1 parent 25e80ee commit 67557e6

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

test/test.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include "../src/websocket.hpp"
33

44
#include <string>
5-
#include <Windows.h>
65

76
const char *opcodes[] = {
87
"Additional", // 0
@@ -18,20 +17,42 @@ const char *opcodes[] = {
1817
using namespace std;
1918
using namespace websocket;
2019

21-
int main(int argc, char **argv) {
22-
tstream::server ser(5333);
20+
class MyHandler : public WebSocketHandler {
21+
public:
22+
static void onopen(MyHandler& h) {
23+
cout << "OPEN: " << h.getHost() << endl;
24+
cout << "GET " << h.getPath() << endl;
25+
cout << "Host: " << h.getHost() << endl;
26+
cout << "Sec-WebSocket-Key: " << h.getKey() << endl;
27+
cout << "Sec-WebSocket-Protocol: " << h.getProtocol() << endl;
28+
}
29+
static void onmessage(MyHandler& h) {
30+
cout << h.data() << endl;
31+
}
32+
static void onclose(MyHandler& h) {
33+
cout << "CLOSED" << endl;
34+
}
35+
};
36+
37+
void test1() {
38+
tstream::server ser("127.0.0.1", 5333);
2339
WebSocketHandler h = ser.accept();
2440
if (h) {
2541
cout << "GET " << h.getPath() << endl;
2642
cout << "Host: " << h.getHost() << endl;
2743
cout << "Sec-WebSocket-Key: " << h.getKey() << endl;
28-
cout << "Sec-WebSocket-Protocol: " << h.getSubProtocol() << endl;
44+
cout << "Sec-WebSocket-Protocol: " << h.getProtocol() << endl;
2945
while (1) {
3046
if (h.recv())
3147
cout << h.data() << endl,
3248
h.send(h.data(), true);
3349
getchar();
3450
}
3551
}
52+
}
53+
54+
int main(int argc, char **argv) {
55+
WebSocketServer<MyHandler> ser("127.0.0.1", 5333);
56+
ser.run();
3657
return 0;
3758
}

test/test.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5+
<script src="./test.js"></script>
6+
<title>test</title>
7+
</head>
8+
<body>
9+
<button onclick="connect()">Connect</button>
10+
</body>
11+
</html>

test/test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function connect() {
2+
ws = new WebSocket('ws://localhost:5333');
3+
// ws.binaryType = 'arraybuffer'
4+
ws.onopen = function(ev) {
5+
console.log('opened');
6+
ws.send('abcdefg');
7+
ws.send('12345678');
8+
ws.send('ABCDEFG');
9+
};
10+
ws.onmessage = function(ev) {
11+
console.log(ev);
12+
last = ev.data;
13+
};
14+
ws.onclose = function(ev) {
15+
console.log('CLOSED');
16+
};
17+
ws.onerror = function(ev) {
18+
console.log('ERROR', ev);
19+
};
20+
}
21+
connect();

test/xmake.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,15 @@
22
target 'test'
33
set_kind 'binary'
44
add_headers '../src/*.hpp'
5-
add_files '../src/*.cpp'
65
add_files 'test.cpp'
6+
7+
add_linkdirs '$(buildir)'
8+
add_links 'websocket'
9+
10+
add_deps 'websocket'
11+
12+
if os.host() ~= 'windows' then
13+
add_cxxflags '-std=c++11'
14+
elseif is_mode 'debug' then
15+
add_cxxflags '/MTd'
16+
end

0 commit comments

Comments
 (0)