-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmessaging.html
95 lines (76 loc) · 2.87 KB
/
messaging.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
<!DOCTYPE html>
<head>
<title>Respoke - Messaging Example</title>
<!-- Respoke client library -->
<script src="https://cdn.respoke.io/respoke.min.js"></script>
<!-- jQuery, for this example -->
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<!-- Some simple styles to make things perty -->
<link rel="stylesheet" type="text/css" href="style.css"></style>
</head>
<body>
<h3 id="status">Not Connected</h3>
<div id="login">
<input id="endpoint" placeholder="Username" type="text" />
<button id="doLogin">Connect</button>
</div>
<div id="messaging">
<ul id="messages"></ul><br />
<input id="remoteId" placeholder="User to Message" type="text" /><br/>
<textarea id="textToSend" placeholder="Message to Send" rows="2"></textarea><br/>
<button id='sendMessage'>Send Message</button>
</div>
<script type="text/javascript">
// App ID value from the dev portal. You can play
// around with the supplied ID but should replace it
// with your own.
var appid = "b4931d40-ff2b-4c46-8487-bf955a75501d";
// The ID that other users will identify you by (your username)
var endpointId;
// Create the client object using the App ID
var client = respoke.createClient({
appId: appid,
developmentMode: true
});
// "connect" event fired after successful connection to Respoke
client.listen('connect', function() {
$("#status").html("Connected to Respoke as \"" + endpointId + "\"");
});
// Listen for incoming messages
client.listen('message', function(evt) {
$("#messages").append(
"<li>" + evt.message.message + "</li>"
);
});
// Connect to Respoke when the user clicks "connect"
$("#doLogin").click(function() {
$("#status").html("Connecting...");
endpointId = $("#endpoint").val();
client.connect({
endpointId: endpointId // your username is the endpoint
});
});
// Send message
$("#sendMessage").click(function() {
// Get the recipients name
var remote = $("#remoteId").val();
// Make an endpoint for that recipient
var endpoint = client.getEndpoint({
id: remote
});
// Grab the text to send
var messageText = $("#textToSend").val();
// Send it
endpoint.sendMessage({
message: messageText
});
// Show yourself the message
$("#messages").append(
"<li>" + messageText + "</li>"
);
// Clear the text you just sent
$("#textToSend").val('');
});
</script>
</body>
</html>