Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Voting
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dennis Willers
Voting
Commits
332b9d78
Commit
332b9d78
authored
Aug 23, 2021
by
Dennis Willers
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change socket.js to new API
parent
93974b59
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
265 additions
and
3744 deletions
+265
-3744
helpFunctions.js
helpFunctions.js
+17
-2
index.js
index.js
+2
-2
package-lock.json
package-lock.json
+210
-3707
package.json
package.json
+1
-0
setVote.js
route/setVote.js
+1
-1
socket.js
socket.js
+34
-32
No files found.
helpFunctions.js
View file @
332b9d78
...
...
@@ -18,7 +18,21 @@ function checkIfRequestHasVote(json) {
return
JSON
.
parse
(
json
).
hasOwnProperty
(
'
vote
'
);
}
function
sendRequest
(
url
,
json
,
callback
)
{
function
sendGetRequest
(
url
,
callback
)
{
//make post request
axios
.
get
(
url
)
.
then
(
res
=>
{
//echo data
callback
(
res
);
})
.
catch
(
error
=>
{
console
.
error
(
error
);
callback
(
null
);
});
}
function
sendPostRequest
(
url
,
json
,
callback
)
{
//make post request
axios
.
post
(
url
,
JSON
.
parse
(
json
))
...
...
@@ -45,6 +59,7 @@ function addMapToClient(clientList, currentClient, mapId, callback) {
module
.
exports
=
{
isJsonString
,
checkIfRequestHasVote
,
sendRequest
,
sendGetRequest
,
sendPostRequest
,
addMapToClient
};
index.js
View file @
332b9d78
...
...
@@ -63,7 +63,7 @@ app.use(function(req, res, next){
if
(
ua
.
toLowerCase
().
includes
(
bUA
.
toLowerCase
())){
blacklisted
=
true
}
})
})
;
if
(
blacklisted
)
{
console
.
log
(
'
BLACKLISTED UA:
'
+
ua
)
...
...
@@ -72,7 +72,7 @@ app.use(function(req, res, next){
next
()
}
}
})
})
;
app
.
get
(
'
/
'
,
(
req
,
res
)
=>
{
let
data
=
{
online
:
true
,
routes
:
[],
docs
:
'
https://git.willers.digital/Dennis.Willers/voting/blob/master/README.md
'
};
...
...
package-lock.json
View file @
332b9d78
This source diff could not be displayed because it is too large. You can
view the blob
instead.
package.json
View file @
332b9d78
...
...
@@ -19,6 +19,7 @@
"
express
"
:
"
^4.17.1
"
,
"
morgan
"
:
"
^1.10.0
"
,
"
mysql
"
:
"
^2.18.1
"
,
"
valid-url
"
:
"
^1.0.9
"
,
"
trackmania.io
"
:
"
^2.4.0
"
},
"devDependencies"
:
{
...
...
route/setVote.js
View file @
332b9d78
...
...
@@ -62,4 +62,4 @@ module.exports = function(app, sql, errorHandler) {
})
.
catch
((
err
)
=>
errorHandler
(
res
,
400
,
err
));
// player not found
});
};
\ No newline at end of file
};
socket.js
View file @
332b9d78
...
...
@@ -72,32 +72,28 @@ server.on('connection',function(socket){
console
.
log
(
'
Data sent to server :
'
+
data
);
if
(
hf
.
isJsonString
(
data
))
{
let
url
;
if
(
hf
.
checkIfRequestHasVote
(
data
))
{
url
=
'
http://localhost:8080/setVote
'
;
}
else
{
url
=
'
http://localhost:8080/getMapInfo
'
;
}
hf
.
sendRequest
(
url
,
data
,
res
=>
{
if
(
res
)
{
const
req
=
JSON
.
parse
(
data
);
hf
.
addMapToClient
(
clients
,
socket
,
req
[
'
mapId
'
],
newClientList
=>
{
const
req
=
JSON
.
parse
(
data
);
if
(
req
[
'
http
'
]
===
'
GET
'
)
{
hf
.
sendGetRequest
(
'
http://localhost:8080/
'
+
req
[
'
url
'
],
res
=>
{
hf
.
addMapToClient
(
clients
,
socket
,
req
[
'
map
'
],
newClientList
=>
{
clients
=
newClientList
.
slice
();
socket
.
write
(
JSON
.
stringify
(
res
.
data
)
+
'
\n
'
);
if
(
hf
.
checkIfRequestHasVote
(
data
))
{
sendToAllClientsAtSameMap
(
res
,
req
[
'
mapId
'
],
socket
);
}
});
//var is_kernel_buffer_full = socket.write(JSON.stringify(res.data) + '\n');
//if(is_kernel_buffer_full){
// console.log('Data was flushed successfully from kernel buffer i.e written successfully!');
//} else {
// socket.pause();
//}
}
else
{
console
.
log
(
"
Res was null
"
);
}
});
})
}
else
if
(
req
[
'
http
'
]
===
'
POST
'
)
{
hf
.
sendPostRequest
(
'
http://localhost:8080/setVote
'
,
data
,
res
=>
{
if
(
res
)
{
hf
.
addMapToClient
(
clients
,
socket
,
req
[
'
map
'
],
newClientList
=>
{
clients
=
newClientList
.
slice
();
socket
.
write
(
JSON
.
stringify
(
res
.
data
)
+
'
\n
'
);
sendToAllClientsAtSameMap
(
req
[
'
map
'
],
socket
);
});
}
else
{
console
.
log
(
"
Res was null
"
);
}
});
}
}
});
...
...
@@ -150,14 +146,16 @@ server.on('connection',function(socket){
}
// Send a message to all clients playing the same map
function
sendToAllClientsAtSameMap
(
res
,
mapId
,
currentClient
)
{
console
.
log
(
"
JSON:
"
,
JSON
.
stringify
(
res
.
data
));
clients
.
forEach
(
function
(
client
)
{
const
resJson
=
res
.
data
;
delete
resJson
[
'
vote
'
];
if
(
client
.
mapId
===
mapId
&&
client
.
name
!==
currentClient
)
client
.
write
(
JSON
.
stringify
(
resJson
)
+
'
\n
'
);
function
sendToAllClientsAtSameMap
(
mapId
,
currentClient
)
{
hf
.
sendGetRequest
(
'
http://localhost:8080/getVotes?map=
'
+
mapId
,
res
=>
{
console
.
log
(
"
JSON:
"
,
JSON
.
stringify
(
res
.
data
));
clients
.
forEach
(
function
(
client
)
{
const
resJson
=
res
.
data
;
delete
resJson
[
'
vote
'
];
if
(
client
.
mapId
===
mapId
&&
client
.
name
!==
currentClient
)
client
.
write
(
JSON
.
stringify
(
resJson
)
+
'
\n
'
);
});
console
.
log
(
"
Write Message to all at
"
+
mapId
+
"
:
"
+
JSON
.
stringify
(
res
.
data
));
});
console
.
log
(
"
Write Message to all at
"
+
mapId
+
"
:
"
+
JSON
.
stringify
(
res
.
data
));
}
});
...
...
@@ -253,11 +251,15 @@ client.on('data',function(data){
// open new node instance => and run it...
// 87.78.129.86 | localhost
// Examples:
// getVotes: {"map":"OkNwgzSwDLZSWSRPsUKY7EA1Cg5","http":"GET","url":"getVotes?map=OkNwgzSwDLZSWSRPsUKY7EA1Cg5"}
// setVotes: {"http":"POST", "map": "OkNwgzSwDLZSWSRPsUKY7EA1Cg5", "player": "Jtmn3kBnSSadky_mLNhp_A", "vote": 50}
// getPlayerVote: {"map":"OkNwgzSwDLZSWSRPsUKY7EA1Cg5","http":"GET","url":"getPlayerVote?map=OkNwgzSwDLZSWSRPsUKY7EA1Cg5&player=Jtmn3kBnSSadky_mLNhp_A"}
/*const testClient = net.connect({host: '87.78.129.86', port: 3201
}, () => {
const
testClient
=
net
.
connect
({
host
:
'
localhost
'
,
port
:
3000
},
()
=>
{
console
.
log
(
'
connected to server!
'
);
//testClient.write('Hello World Client!\r\n');
testClient.write(JSON.stringify({"
mapId":"7fsfRSUCQ7YwfBEdRk_GivW6qzj","userId":"aKBNg9fbReqLedLiz1KFfA","name":"Summer 2021 - 01"
}));
testClient
.
write
(
JSON
.
stringify
({
"
http
"
:
"
POST
"
,
"
map
"
:
"
OkNwgzSwDLZSWSRPsUKY7EA1Cg5
"
,
"
player
"
:
"
Jtmn3kBnSSadky_mLNhp_A
"
,
"
vote
"
:
80
}));
//test(testClient);
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment