chat-point 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +144 -0
- package/package.json +24 -0
- package/protocol.js +19 -0
package/index.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
#!usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const inquirer= require('inquirer');
|
|
4
|
+
const wsc= require('./protocol.js');
|
|
5
|
+
let isMessage= false;
|
|
6
|
+
async function run(){
|
|
7
|
+
const answer= await inquirer.prompt(
|
|
8
|
+
[
|
|
9
|
+
{
|
|
10
|
+
type:'input',
|
|
11
|
+
name:'name',
|
|
12
|
+
message:'what is your name?'
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
type:'confirm',
|
|
16
|
+
name:'create',
|
|
17
|
+
message:'do you want to create a room?'
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
type:'input',
|
|
21
|
+
name:'room',
|
|
22
|
+
message:'Enter the room name',
|
|
23
|
+
when:(answer)=>answer.create
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
type:'input',
|
|
27
|
+
name:'room',
|
|
28
|
+
message:'Enter the room you want to join:',
|
|
29
|
+
when:(answer)=>!answer.create
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
)
|
|
33
|
+
if(answer.create){
|
|
34
|
+
if(wsc.readyState===1){
|
|
35
|
+
wsc.send(
|
|
36
|
+
JSON.stringify(
|
|
37
|
+
{
|
|
38
|
+
type:'create',
|
|
39
|
+
room:answer.room
|
|
40
|
+
}
|
|
41
|
+
)
|
|
42
|
+
)
|
|
43
|
+
console.log('room created successfully');
|
|
44
|
+
console.log('you can start messaging');
|
|
45
|
+
wsc.on('message', (data)=>{
|
|
46
|
+
isMessage=true;
|
|
47
|
+
const value= JSON.parse(data);
|
|
48
|
+
|
|
49
|
+
if(value){
|
|
50
|
+
console.log(value.name+': '+value.content);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
await chatting(answer);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
}
|
|
59
|
+
if(!answer.create){
|
|
60
|
+
if(wsc.readyState===1){
|
|
61
|
+
wsc.send(JSON.stringify(
|
|
62
|
+
{
|
|
63
|
+
type:'join',
|
|
64
|
+
room:answer.room
|
|
65
|
+
}
|
|
66
|
+
))
|
|
67
|
+
console.log('you have joined the room successfully');
|
|
68
|
+
console.log('you can start messaging');
|
|
69
|
+
wsc.on('message', (data)=>{
|
|
70
|
+
isMessage=true;
|
|
71
|
+
const value= JSON.parse(data);
|
|
72
|
+
|
|
73
|
+
if(value){
|
|
74
|
+
console.log(value.name+': '+value.content);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
})
|
|
79
|
+
await chatting(answer);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// console.log(answer.name);
|
|
86
|
+
// console.log(answer.create);
|
|
87
|
+
// console.log(answer.join);
|
|
88
|
+
// console.log(answer.room);
|
|
89
|
+
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// const prompt= inquirer.createPromptModule();
|
|
93
|
+
// prompt([
|
|
94
|
+
// {
|
|
95
|
+
// type:'input',
|
|
96
|
+
// name:'name',
|
|
97
|
+
// message:'what is your name?'
|
|
98
|
+
// }
|
|
99
|
+
// ]).then((answer)=>{
|
|
100
|
+
// console.log(answer.name);
|
|
101
|
+
// })
|
|
102
|
+
|
|
103
|
+
// prompt([
|
|
104
|
+
// {
|
|
105
|
+
// type:'input',
|
|
106
|
+
// name:'age',
|
|
107
|
+
// message:'what is your age?'
|
|
108
|
+
// }
|
|
109
|
+
// ]).then((answer)=>{
|
|
110
|
+
// console.log(answer.age);
|
|
111
|
+
// })
|
|
112
|
+
async function chatting(user){
|
|
113
|
+
while(true){
|
|
114
|
+
if(isMessage){
|
|
115
|
+
isMessage=false;
|
|
116
|
+
continue;
|
|
117
|
+
|
|
118
|
+
}
|
|
119
|
+
const message= await inquirer.prompt([
|
|
120
|
+
{
|
|
121
|
+
type:'input',
|
|
122
|
+
name:'client',
|
|
123
|
+
message:' '
|
|
124
|
+
}
|
|
125
|
+
])
|
|
126
|
+
// console.log(
|
|
127
|
+
// {
|
|
128
|
+
// name:user.name,
|
|
129
|
+
// message:message.client,
|
|
130
|
+
// room:user.room
|
|
131
|
+
// }
|
|
132
|
+
// )
|
|
133
|
+
wsc.send(JSON.stringify({
|
|
134
|
+
type:'message',
|
|
135
|
+
message:{
|
|
136
|
+
name:user.name,
|
|
137
|
+
content:message.client
|
|
138
|
+
},
|
|
139
|
+
room:user.room
|
|
140
|
+
}))
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
run();
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "chat-point",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
},
|
|
8
|
+
"bin": {
|
|
9
|
+
"chat": "index.js"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"chat",
|
|
13
|
+
"cli",
|
|
14
|
+
"chatting",
|
|
15
|
+
"tool"
|
|
16
|
+
],
|
|
17
|
+
"author": "SahilSingh_108",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"description": "you can chat through terminal alongside of making project",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"inquirer": "^8.2.5",
|
|
22
|
+
"ws": "^8.19.0"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/protocol.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const WebSocket= require('ws');
|
|
2
|
+
|
|
3
|
+
const wsc= new WebSocket('ws://localhost:8080');
|
|
4
|
+
|
|
5
|
+
// wsc.on('open',()=>{
|
|
6
|
+
// console.log('connection is established');
|
|
7
|
+
// })
|
|
8
|
+
// wsc.on('message',(data)=>{
|
|
9
|
+
// console.log(data.toString());
|
|
10
|
+
// })
|
|
11
|
+
// wsc.on('close',()=>{
|
|
12
|
+
// console.log('connection is closed now');
|
|
13
|
+
|
|
14
|
+
// })
|
|
15
|
+
// wsc.on('error',(err)=>{
|
|
16
|
+
// console.log('some error has been arises ',err)
|
|
17
|
+
// })
|
|
18
|
+
|
|
19
|
+
module.exports=wsc;
|