-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket-connect.html
61 lines (51 loc) · 1.66 KB
/
socket-connect.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
<link rel="import" href="../polymer/polymer.html">
<dom-module id="socket-connect">
<template>
<script src$="{{socketIp}}/socket.io/socket.io.js"
on-load="_socketSetup"
on-error="_onScriptError"></script>
<content></content>
</template>
<script>
/**
* The `socket-connect` element registers a connection with a socket.io server.
*
* It also connects with children elements of types `socket-emit` and `socket-on` in order to
* emit data with events as well as receive data from emitted events from the server.
*
* Example:
*
* <socket-connect socket-ip="http://localhost:8000">
* <socket-on name="demo" data="{{onData}}"></socket-on>
* <socket-emit name="demo" data="[[emittedData]]"></socket-emit>
* </socket-connect>
*
* @demo demo/index.html
*/
Polymer({
is: 'socket-connect',
properties: {
/**
* `socketIp` provides the IP or domain address to connect to via io.connect("socketIp")
*/
socketIp: String,
/**
* `token` is an optional attribute specifying a token passed with every socket event
*/
token: String
},
_socketSetup: function(){
this.socket = io.connect(this.socketIp);
var children = Polymer.dom(this).children;
for (var i = 0; i < children.length; i++) {
if (typeof children[i]._setup === 'function') {
children[i]._setup(this.socket, this.token);
}
}
},
_onScriptError: function(err){
console.log(err);
},
});
</script>
</dom-module>