-
Notifications
You must be signed in to change notification settings - Fork 3
/
socket-io.html
140 lines (129 loc) · 3.72 KB
/
socket-io.html
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
<link rel="import" href="../polymer/polymer.html">
<!--
`socket-io`
Polymer 1.0 element for socket.io apis
@demo demo/index.html
-->
<dom-module id="socket-io">
<template>
</template>
<script>
Polymer({
is: 'socket-io',
properties: {
endpoint: {
type: String,
value: 'http://localhost:8080'
},
isConnected: {
type: Boolean,
value: null
}
},
ready: function(){
console.log("Connection In Progress");
var head = document.getElementsByTagName("head")[0] || document.documentElement;
var script = document.createElement("script");
script.src = this._getClientUrl(this.endpoint);
// Handle Script loading
var done = false;
// Attach handlers for all browsers
var _this = this;
script.onload = script.onreadystatechange = function() {
if ( !done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") ) {
done = true;
_this.scriptLoaded = true;
console.log('Finished Loading Socket-IO Script');
if (_this.awaitingConnect) {
console.log("Connecting Socket");
_this.connect();
}
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
if ( head && script.parentNode ) {
head.removeChild( script );
}
}
};
// Use insertBefore instead of appendChild to circumvent an IE6 bug.
// This arises when a base node is used (#2709 and #4378).
head.insertBefore( script, head.firstChild );
},
_getClientUrl: function (endpoint) {
if (endpoint.slice(-1) === '/') {
return endpoint + 'socket.io/socket.io.js';
}
return endpoint + '/socket.io/socket.io.js';
},
/**
* The emit method sends an event with 'payload' as content
*
* @method emit
* @param {String} event event to send
* @param {Object} payload the json data to send
*/
emit: function(event, data)
{
try
{
if(this.socket) this.socket.emit(event, data);
}catch(error)
{
console.log("Failed to send message: "+error);
}
},
/**
* Update the message within state
*
* @method message
*/
_updateMessage: function(message){
if(typeof message !== "object"){
this.outMessage = {message: message};
} else{
this.outMessage = message;
}
},
/**
* The connect method triggers connection to the socket.io server
*
* @method connect
*/
connect: function()
{
var _this = this;
console.log('Connection in progress');
if (this.scriptLoaded) {
this.disconnect();
this.socket = io.connect(this.endpoint);
this.isConnected = true;
this.events.forEach(function(event){
_this.socket.on(event.event, event.onEvent);
});
this.fire('connected');
} else {
console.log('Loading ClientSideScript');
this.awaitingConnect = true;
}
},
/**
* The disconnect method triggers disconnection from the socket.io server
*
* @method disconnect
*/
disconnect: function ()
{
if (this.socket != undefined && !this.isConnected)
{
try{
this.socket.disconnect();
this.isConnected = false;
}
catch(error){
this.onError(error);
}
}
},
});
</script>
</dom-module>