webjars 编写一个websocket美化的index.html页面
时间: 2024-03-03 22:53:41 浏览: 135
好的,以下是一个使用webjars引入jQuery和Bootstrap,并美化WebSocket的index.html页面的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebSocket 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 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 class="col">
<div class="card">
<div class="card-header">WebSocket Messages</div>
<div class="card-body">
<ul id="messageList" class="list-group"></ul>
</div>
</div>
</div>
</div>
</div>
<script>
var webSocket;
function connect() {
var url = $('#urlInput').val();
if (webSocket != null) {
webSocket.close();
}
webSocket = new WebSocket(url);
webSocket.onopen = function(event) {
console.log('WebSocket opened: ' + url);
}
webSocket.onmessage = function(event) {
console.log('WebSocket message received: ' + event.data);
$('#messageList').append('<li class="list-group-item">' + event.data + '</li>');
}
webSocket.onclose = function(event) {
console.log('WebSocket closed: ' + url);
}
}
function disconnect() {
if (webSocket != null) {
webSocket.close();
}
}
</script>
</body>
</html>
```
在这个示例中,我们使用Bootstrap的表单和卡片组件来美化页面,并使用jQuery来处理WebSocket的连接和消息接收。你可以将这个示例代码复制到一个名为`index.html`的文件中,然后将其与WebSocket服务器一起使用。
阅读全文