webjars 编写一个websocket美化的聊天框index.html页面
时间: 2024-03-03 07:53:42 浏览: 126
webSocket实现的页面聊天
好的,以下是一个使用webjars引入jQuery和Bootstrap,并美化WebSocket聊天框的index.html页面的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebSocket Chat Example</title>
<!-- 引入webjars中的jQuery和Bootstrap -->
<link rel="stylesheet" href="/webjars/bootstrap/5.1.0/css/bootstrap.min.css">
<script src="/webjars/jquery/3.6.0/jquery.min.js"></script>
<script src="/webjars/bootstrap/5.1.0/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-5">
<h1>WebSocket Chat Example</h1>
<hr>
<div class="row">
<div class="col">
<form>
<div class="form-group">
<label for="urlInput">WebSocket URL</label>
<input type="text" class="form-control" id="urlInput" placeholder="Enter WebSocket URL">
</div>
<button type="button" class="btn btn-primary" onclick="connect()">Connect</button>
<button type="button" class="btn btn-danger" onclick="disconnect()">Disconnect</button>
</form>
</div>
</div>
<div class="row mt-5">
<div class="col-3">
<div class="card">
<div class="card-header">Users</div>
<div class="card-body">
<ul id="userList" class="list-group"></ul>
</div>
</div>
</div>
<div class="col-9">
<div class="card">
<div class="card-header">Chat</div>
<div class="card-body">
<ul id="messageList" class="list-group"></ul>
</div>
<div class="card-footer">
<form>
<div class="form-group">
<input type="text" class="form-control" id="messageInput" placeholder="Enter message">
</div>
<button type="button" class="btn btn-primary" onclick="sendMessage()">Send</button>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
var webSocket;
var username;
function connect() {
var url = $('#urlInput').val();
if (webSocket != null) {
webSocket.close();
}
webSocket = new WebSocket(url);
webSocket.onopen = function(event) {
console.log('WebSocket opened: ' + url);
username = prompt('Enter your username:');
webSocket.send(username + ' has joined the chat.');
}
webSocket.onmessage = function(event) {
console.log('WebSocket message received: ' + event.data);
var message = event.data.split(':');
var sender = message[0].trim();
var content = message[1].trim();
if (content == 'has joined the chat.' || content == 'has left the chat.') {
$('#userList').empty();
var users = content.split(',');
for (var i = 0; i < users.length; i++) {
$('#userList').append('<li class="list-group-item">' + users[i].trim() + '</li>');
}
} else {
$('#messageList').append('<li class="list-group-item"><strong>' + sender + ':</strong> ' + content + '</li>');
}
}
webSocket.onclose = function(event) {
console.log('WebSocket closed: ' + url);
$('#userList').empty();
$('#messageList').empty();
}
}
function disconnect() {
if (webSocket != null) {
webSocket.close();
}
}
function sendMessage() {
var message = $('#messageInput').val();
if (webSocket != null && message != '') {
webSocket.send(username + ': ' + message);
$('#messageInput').val('');
}
}
</script>
</body>
</html>
```
在这个示例中,我们使用Bootstrap的卡片和表单组件来美化页面,并使用jQuery来处理WebSocket的连接、消息接收和消息发送。聊天框中显示了在线用户列表和消息列表,并提供了一个表单来输入和发送新消息。你可以将这个示例代码复制到一个名为`index.html`的文件中,然后将其与WebSocket服务器一起使用。
阅读全文