-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver1.js
197 lines (118 loc) · 3.99 KB
/
server1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const express = require('express')
const app1 = express()
const http = require('http').createServer(app1)
const PORT1 = process.env.PORT || 5000
http.listen(PORT1, () => {
console.log(`Listening on port ${PORT1}`)
})
app1.use(express.static(__dirname + '/public'))
app1.get('/', (req, res) => {
res.sendFile(__dirname +'/public' + '/index.html')
})
// const secureKey = 9211 ;
// Socket
const io = require('socket.io')(http)
// store player names : (must be unique)
const allUsers = []
const players = []
block = false ;
const socketArray = []
check = true
var turn = 0
io.on('connection', (socket) => {
console.log('A user connected ...');
socket.on('user-data',(content)=>{
console.log(content.user + ' joined ');
players.push({
serverID : socket.id ,
clientID : content.ID,
name : content.user
})
// console.log(socketArray[0] + "this one");
console.log({
serverID : socket.id ,
clientID : content.ID,
name : content.user
})
socketArray.push(socket) ;
allUsers.push({
user : content.user,
ID : content.ID
})
// players[socket.id] = content.user ;
// players.push(content.user);
console.log(socket.id);
socket.broadcast.emit('user-data',allUsers) ;
socket.emit('user-data',allUsers )
})
socket.on('message',(content)=>{
socket.broadcast.emit('message',content) ;
// socketArray[2].emit('message',content) ;
})
// receiving and sending private chats
socket.on('private-message',(content,members)=>{
console.log("privacy activated");
// console.log(members);
// console.log(players);
// socketArray[1].broadcast.emit('private-message',content) ;
for(var i = 0; i< members.length; i++)
{
var x = members[i]
for(var y = 0; y < allUsers.length; y++)
{
if( players[y].clientID == x)
{
socketArray[y].emit('private-message',content) ;
}
}
}
})
socket.on('disconnect',()=>{
console.log(` disconnected ...`);
for (let player of players) {
if (player.serverID === socket.id) {
socket.broadcast.emit('left', player.clientID);
console.log(player);
for(i = 0; i < allUsers.length; i++)
{
if(allUsers[i].ID == player.clientID)
{
allUsers.splice(i,1) ; // remove user data form array
socketArray.splice(i,1); // remove socket form array
break ;
}
}
break; // assuming there is only one matching player, we can break out of the loop once we find it
}
}
// console.log(players);
})
socket.on('typing',(content)=>{
socket.broadcast.emit('typing',content) ;
})
socket.on('not-typing',(content)=>{
socket.broadcast.emit('not-typing',content) ;
})
})
app1.post('/', async(req,res)=>{
try {
const prompt = req.body.prompt;
console.log(prompt);
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
console.log(text);
res.status(200).send({
bot: text
});
}
catch(err)
{
res.status(400).send("unable to connect...")
}
})
// app1.listen(PORT1,(err)=>{
// if(!err)
// console.log(`server running at port ${port}`);
// })