-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathchat.html
More file actions
133 lines (120 loc) · 4.14 KB
/
Copy pathchat.html
File metadata and controls
133 lines (120 loc) · 4.14 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="./css/style.css">
<script src='https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js'></script>
<script src='./js/jquery.mCustomScrollbar.concat.min.js'></script>
<script src="./js/paho-mqtt.js" type="text/javascript"></script>
<link rel='stylesheet prefetch' href='https://fonts.googleapis.com/css?family=Open+Sans'>
<link rel='stylesheet prefetch' href='./js/jquery.mCustomScrollbar.min.css'>
<title>Coolpy7ChatDemo</title>
</head>
<body>
<div class="chat">
<div class="chat-title">
<h1>Coolpy 7</h1>
<h2>Coolpy.net</h2>
<figure class="avatar">
<img src="assets/coolgirl.jpg"/>
</figure>
</div>
<div class="messages">
<div class="messages-content"></div>
</div>
<div class="message-box">
<textarea type="text" class="message-input" placeholder="Type message..."></textarea>
<button type="submit" class="message-submit">Send</button>
</div>
</div>
<div class="bg"></div>
<script src="./js/chat.js"></script>
<script>
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var uid = getParameterByName('uid');
if (uid.length < 1) {
window.location.href = '/';
}
$(document).ready(function () {
var topic = "coolpy/chatroom";
// Create a client instance
client = new Paho.Client("127.0.0.1", Number(8083), "/", uid);
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.onConnected = function () {
message = new Paho.Message(JSON.stringify({"uid": uid, "cmd": "add"}));
message.destinationName = topic;
client.send(message);
};
// called when the client connects
function onConnect() {
client.subscribe(topic, {qos: 0});
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:" + responseObject.errorMessage);
}
}
// called when a message arrives
function onMessageArrived(message) {
var m = JSON.parse(message.payloadString);
if (m.cmd === "add") {
fakeMessage(m.uid + "进入聊天室。。。")
}
if (m.cmd === "msg") {
if (m.uid !== uid) {
fakeMessage(m.msg)
} else {
insertMessage(m.msg)
}
}
}
function connect() {
// connect the client
client.connect({
onSuccess: onConnect,
cleanSession: true,
useSSL: false,
keepAliveInterval: 60
});
}
connect();
/**
* @return {boolean}
*/
function Send(msg) {
msg = $('.message-input').val();
if ($.trim(msg) === '') {
return false;
}
$('.message-input').val(null);
message = new Paho.Message(JSON.stringify({"uid": uid, "cmd": "msg", "msg": msg}));
message.destinationName = topic;
message.qos = 0;
client.send(message);
}
$('.message-submit').click(function () {
Send()
});
$(window).on('keydown', function (e) {
if (e.which === 13) {
Send();
return false;
}
});
});
</script>
</body>
</html>