1+ <!DOCTYPE html>
2+ < html >
3+ < head >
4+ < title > Real Time Polling App using Socket.io</ title >
5+ </ head >
6+ < body >
7+
8+ <!-- Import dependencies -->
9+ <!-- https://cdnjs.com/libraries/socket.io -->
10+ <!-- https://cdnjs.com/libraries/Chart.js -->
11+ <!-- https://getbootstrap.com/docs/5.0/getting-started/introduction -->
12+ < script src ="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.1/socket.io.min.js " integrity ="sha512-gVG6WRMUYFaIdoocaxbqd02p3DUbhReTBWc7NTGB96i7vONrr7GuCZJHFFmkXEhpwilIWUnfRIMSlKaApwd/jg== " crossorigin ="anonymous "> </ script >
13+ < script src ="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js " integrity ="sha512-d9xgZrVZpmmQlfonhQUvTR7lMPtO7NkZMkA0ABN3PHCbKA5nqylQ/yWlFAyY6hYgdF1Qh6nYiuADWwKB4C2WSw== " crossorigin ="anonymous "> </ script >
14+ < script src ="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js " integrity ="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW " crossorigin ="anonymous "> </ script >
15+
16+ < link href ="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css " rel ="stylesheet " integrity ="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1 " crossorigin ="anonymous ">
17+
18+ < link rel ="stylesheet " href ="style.css ">
19+
20+ < div class ="container ">
21+ < div class ="row ">
22+
23+ <!-- The title -->
24+ < h1 class ="text-center "> What is the best programming language?</ h1 >
25+
26+ <!-- The canvas -->
27+ < canvas id ="voteChart " style ="height:50vh; width:100vw "> </ canvas >
28+
29+ <!-- The buttons -->
30+ < div class ="btn-group center " role ="group ">
31+ < button type ="button " id ="button1 " class ="btn " onclick ="vote(0) "> JavaScript</ button >
32+ < button type ="button " id ="button2 " class ="btn " onclick ="vote(1) "> C#</ button >
33+ < button type ="button " id ="button3 " class ="btn " onclick ="vote(2) "> Python</ button >
34+ </ div >
35+
36+ </ div >
37+ </ div >
38+
39+ < script >
40+
41+ // Initialize the canvas
42+ const ctx = document . getElementById ( "voteChart" ) . getContext ( "2d" ) ;
43+
44+ // Initialize the chart
45+ const chart = new Chart ( ctx , {
46+ type : "bar" ,
47+ data : {
48+ labels : [ "Candidates" ] ,
49+ } ,
50+ options : {
51+
52+ }
53+ } ) ;
54+
55+ // Connect to the server it's (Here you want to change the port if you changed it in the server)
56+ const socket = io ( "http://192.168.1.4:80" ) ;
57+
58+ // On new vote update the chart
59+ socket . on ( "update" , ( candidates ) => {
60+
61+ // Convert the candidates object into an array
62+ candidates = Object . entries ( candidates ) ;
63+
64+ // For each candidate
65+ for ( const [ key , candidate ] of candidates ) {
66+
67+ // Update the vote if the candidate already exists if not create a new candidate and then update the vote
68+ if ( typeof chart . data . datasets [ key ] == "undefined" && chart . data . datasets . length < candidates . length ) {
69+ chart . data . datasets . push ( {
70+ backgroundColor : candidate . color ,
71+ borderColor : candidate . color ,
72+ data : [ candidate . votes ] ,
73+ label : candidate . label
74+ } ) ;
75+ } else if ( typeof chart . data . datasets [ key ] != "undefined" ) {
76+ chart . data . datasets [ key ] . data = [ candidate . votes ] ;
77+ }
78+
79+ }
80+
81+ // Update the chart
82+ chart . update ( ) ;
83+ } ) ;
84+
85+ // Make a new vote (Remember this is not a safe way to do this)
86+ function vote ( index ) {
87+ socket . emit ( "vote" , index ) ;
88+ }
89+
90+ </ script >
91+ </ body >
92+ </ html >
0 commit comments