-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
125 lines (96 loc) · 3.18 KB
/
server.js
File metadata and controls
125 lines (96 loc) · 3.18 KB
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
const express =require("express");
const io = require('socket.io')();
const UserFunctions=require('./classes/User')
const CakeFunctions=require('./classes/Cake')
const app=express();
http=require('http');
const server=http.createServer(app);
const mongoose=require('mongoose');
const IP="127.0.0.1";
const PORT=4000;
const IO_PORT=4500;
const DATABASE_CONNECTION="mongodb://localhost:27017/cakeDB"
let socketArray=[]
let clientArray=[]
let serviceProviderArray=[]
exports.addToClientArray=(client)=>{
clientArray.push(client);
}
exports.addToServiceProviderArray=(client)=>{
serviceProviderArray.push(client);
}
app.use(express.static(__dirname+'/public/build'));
io.on('connection', (client) => { //a new client has been connected
socketArray.push(client)
console.log("connected");
client.emit("Connected")
client.on("loginData",(data)=>{
console.log("data : "+ JSON.stringify(data));
UserFunctions.signUpFunction(data, client);
})
client.on("Login",(data)=>{
console.log("data : "+ JSON.stringify(data));
let user= UserFunctions.loginFunction(data, client);
// if(user){
// client.userData=user;
// }
})
client.on("SubmitCake",(data)=>{
console.log("data : "+ JSON.stringify(data));
CakeFunctions.saveCustomCake(data, client);
})
client.on("Uploading Image",(data)=>{
client.emit("Done Uploading Image")
if(client.type==="customer"){
if(client.userData){
CakeFunctions.saveCustomizedCakePhoto(data,client)
}
}
})
client.on("Uploading Cake Image",(data)=>{
client.emit("Done Uploading Cake Image")
if(client.type==="serviceProvider"){
if(client.userData){
CakeFunctions.saveCakeShopUploadedPhoto(data,client,socketArray)
}
}
})
client.on("disconnect",()=>{ //to remove a client form socketArray.Now only the online clients are here
socketArray.splice(socketArray.indexOf(client),1)
clientArray.splice(clientArray.indexOf(client),1)
serviceProviderArray.splice(serviceProviderArray.indexOf(client),1)
console.log(serviceProviderArray)
})
client.on("GET_SHOPS",()=>{
UserFunctions.loadShops(client)
})
client.on("GET_CAKES",(data)=>{
UserFunctions.loadCakes(client,data)
})
client.on("GET_OFFTHESHELF",(data)=>{
UserFunctions.loadOffTheShelf(client,data)
})
client.on("SET_ORDER",(data)=>{
UserFunctions.saveShelfCakeOrder(client,data)
})
//Service Provider orders
client.on("GET_ORDERS", ()=> {
if(client.type==="serviceProvider"){
if(client.userData){
CakeFunctions.getOrdersServiceProvider(client)
}
}
})
client.on("GET_ORDERS_USER", ()=> {
if(client.type==="customer"){
if(client.userData){
CakeFunctions.getOrdersClient(client);
}
}
})
})
mongoose.Promise=global.Promise;
mongoose.connect(DATABASE_CONNECTION, {useNewUrlParser: true});
console.log("running");
io.listen(IO_PORT);
server.listen(PORT,IP);