Skip to content

Commit 363d4ef

Browse files
author
Wasin Waeosri
committed
create init version for testing concept
0 parents  commit 363d4ef

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
greeter.ts
2+
greeter.js
3+
package.json
4+
node_modules/
5+
greeter.html

test.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head><title>TypeScript WebSocket Tester</title></head>
4+
<script src="./test.js"></script>
5+
<body>5555
6+
<div id="msg">
7+
8+
</div>
9+
</body>
10+
</html>

test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var ws = null;
2+
var url = "ws://172.20.33.11:17000/WebSocket";
3+
var protocol = "tr_json2";
4+
var loginID = 1;
5+
var loginDomain = "Login";
6+
var LoginKeyElements = /** @class */ (function () {
7+
function LoginKeyElements(ApplicationId, Position) {
8+
this.ApplicationId = ApplicationId;
9+
this.Position = Position;
10+
}
11+
return LoginKeyElements;
12+
}());
13+
var LoginMsgKey = /** @class */ (function () {
14+
function LoginMsgKey(Name, ApplicationId, Position) {
15+
this.Name = Name;
16+
var loginKeyElements_class = new LoginKeyElements(ApplicationId, Position);
17+
}
18+
return LoginMsgKey;
19+
}());
20+
var LoginMsg = /** @class */ (function () {
21+
function LoginMsg(Id, Name, ApplicationId, Position) {
22+
this.Domain = loginDomain;
23+
this.Id = Id;
24+
var loginMsgKey_class = new LoginMsgKey(Name, ApplicationId, Position);
25+
this.Key = loginMsgKey_class;
26+
}
27+
return LoginMsg;
28+
}());
29+
function connect() {
30+
ws = new WebSocket(url, protocol);
31+
ws.onopen = onOpen;
32+
ws.onmessage = onMessage;
33+
ws.onerror = onError;
34+
ws.onclose = onClose;
35+
}
36+
function onOpen(event) {
37+
console.log("connected");
38+
sendLogin();
39+
}
40+
function onMessage(event) {
41+
console.log(JSON.stringify(event.data));
42+
}
43+
function onError(event) {
44+
console.log(JSON.stringify(event.data));
45+
}
46+
;
47+
function onClose(event) {
48+
console.log(JSON.stringify(event.data));
49+
}
50+
;
51+
function sendLogin() {
52+
var login = new LoginMsg(loginID, "wasin", "777", "127.0.0.1");
53+
ws.send(JSON.stringify(login));
54+
}
55+
window.onload = function () {
56+
console.log("onLoad");
57+
connect();
58+
};

test.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
let ws:any = null;
2+
let url: string = "ws://172.20.33.11:17000/WebSocket";
3+
const protocol: string = "tr_json2";
4+
const loginID: number = 1;
5+
const loginDomain: string = "Login";
6+
7+
interface JSONLogin {
8+
Id:number;
9+
Domain:string;
10+
Key: JSONLoginKey;
11+
}
12+
13+
interface JSONLoginKey{
14+
Elements: JSONLoginKeyElements;
15+
Name: string;
16+
}
17+
18+
interface JSONLoginKeyElements{
19+
ApplicationId: string;
20+
Position: string;
21+
}
22+
23+
class LoginKeyElements implements JSONLoginKeyElements{
24+
ApplicationId: string;
25+
Position: string;
26+
constructor(ApplicationId: string, Position: string){
27+
this.ApplicationId = ApplicationId;
28+
this.Position = Position;
29+
}
30+
31+
}
32+
33+
class LoginMsgKey implements JSONLoginKey{
34+
Name: string;
35+
Elements: JSONLoginKeyElements;
36+
constructor(Name:string, ApplicationId: string, Position: string){
37+
this.Name = Name;
38+
let loginKeyElements_class = new LoginKeyElements(ApplicationId,Position);
39+
}
40+
}
41+
42+
class LoginMsg implements JSONLogin{
43+
Id:number;
44+
Domain:string = loginDomain;
45+
Key: JSONLoginKey;
46+
47+
constructor(Id:number,Name:string, ApplicationId: string, Position: string){
48+
this.Id = Id;
49+
let loginMsgKey_class = new LoginMsgKey(Name,ApplicationId,Position);
50+
this.Key = loginMsgKey_class;
51+
}
52+
}
53+
54+
55+
function connect(): void{
56+
ws = new WebSocket(url,protocol);
57+
ws.onopen = onOpen;
58+
ws.onmessage = onMessage;
59+
ws.onerror = onError;
60+
ws.onclose = onClose;
61+
}
62+
63+
function onOpen(event:any): void{
64+
console.log("connected");
65+
sendLogin();
66+
}
67+
68+
function onMessage(event:any): void {
69+
console.log(JSON.stringify(event.data));
70+
}
71+
72+
function onError(event:any): void {
73+
console.log(JSON.stringify(event.data));
74+
};
75+
76+
function onClose(event:any): void {
77+
console.log(JSON.stringify(event.data));
78+
};
79+
80+
function sendLogin():void {
81+
let login = new LoginMsg(loginID,"wasin","777","127.0.0.1");
82+
ws.send(JSON.stringify(login));
83+
}
84+
85+
window.onload = function(){
86+
console.log("onLoad");
87+
connect();
88+
89+
};
90+
91+
92+

tsconfig.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"compilerOptions": {
3+
/* Basic Options */
4+
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
5+
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6+
// "lib": [], /* Specify library files to be included in the compilation: */
7+
// "allowJs": true, /* Allow javascript files to be compiled. */
8+
// "checkJs": true, /* Report errors in .js files. */
9+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
10+
// "declaration": true, /* Generates corresponding '.d.ts' file. */
11+
// "sourceMap": true, /* Generates corresponding '.map' file. */
12+
// "outFile": "./", /* Concatenate and emit output to single file. */
13+
// "outDir": "./", /* Redirect output structure to the directory. */
14+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
15+
// "removeComments": true, /* Do not emit comments to output. */
16+
// "noEmit": true, /* Do not emit outputs. */
17+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
18+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
19+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
20+
21+
/* Strict Type-Checking Options */
22+
"strict": true /* Enable all strict type-checking options. */
23+
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
24+
// "strictNullChecks": true, /* Enable strict null checks. */
25+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
26+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
27+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
28+
29+
/* Additional Checks */
30+
// "noUnusedLocals": true, /* Report errors on unused locals. */
31+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
32+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
33+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
34+
35+
/* Module Resolution Options */
36+
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
37+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
38+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
39+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
40+
// "typeRoots": [], /* List of folders to include type definitions from. */
41+
// "types": [], /* Type declaration files to be included in compilation. */
42+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
43+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
44+
45+
/* Source Map Options */
46+
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
47+
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
48+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
49+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
50+
51+
/* Experimental Options */
52+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
53+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
54+
}
55+
}

0 commit comments

Comments
 (0)