提供一个微信小程序和springboot长链接,后端IP是 192.168.124.17 端口号是8080,写一个微信小程序的demo
时间: 2023-07-16 11:14:50 浏览: 158
微信小程序茶叶商城(含后端)示列源码, 微信小程序 茶叶商城 示列源码 后台node.js
5星 · 资源好评率100%
以下是一个简单的微信小程序demo,使用长链接与Spring Boot后端通信:
1. 在微信开发者工具中创建一个新的小程序项目,填写AppID等信息。
2. 在pages文件夹下创建一个名为index的页面,编辑index.json文件,填写如下代码:
```
{
"navigationBarTitleText": "Demo"
}
```
3. 编辑index.wxml文件,填写如下代码:
```
<view class="container">
<view class="title">{{title}}</view>
<view class="content">{{content}}</view>
</view>
```
4. 编辑index.js文件,填写如下代码:
```
Page({
data: {
title: '',
content: ''
},
onLoad: function () {
var socketOpen = false
var socketMsgQueue = []
wx.connectSocket({
url: 'ws://192.168.124.17:8080/ws',
success: function (res) {
console.log('WebSocket连接已打开!')
socketOpen = true
for (var i = 0; i < socketMsgQueue.length; i++) {
sendSocketMessage(socketMsgQueue[i])
}
socketMsgQueue = []
}
})
wx.onSocketOpen(function (res) {
console.log('WebSocket连接已打开!')
socketOpen = true
for (var i = 0; i < socketMsgQueue.length; i++) {
sendSocketMessage(socketMsgQueue[i])
}
socketMsgQueue = []
})
wx.onSocketMessage(function (res) {
console.log('收到服务器内容:' + res.data)
var data = JSON.parse(res.data)
this.setData({
title: data.title,
content: data.content
})
})
wx.onSocketClose(function (res) {
console.log('WebSocket连接已关闭!')
socketOpen = false
})
function sendSocketMessage(msg) {
if (socketOpen) {
wx.sendSocketMessage({
data: msg
})
} else {
socketMsgQueue.push(msg)
}
}
}
})
```
5. 在Spring Boot后端中创建一个WebSocketController,代码如下:
```
@Controller
public class WebSocketController {
@Autowired
private SimpMessagingTemplate messagingTemplate;
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
Thread.sleep(1000); // simulated delay
return new Greeting("Hello, " + message.getName() + "!");
}
}
```
6. 在Spring Boot后端中创建一个Greeting类,代码如下:
```
public class Greeting {
private String content;
public Greeting(String content) {
this.content = content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
```
7. 在Spring Boot后端中创建一个HelloMessage类,代码如下:
```
public class HelloMessage {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
```
8. 在Spring Boot后端的配置文件application.properties中添加以下内容:
```
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
```
9. 在Spring Boot后端中创建一个WebSocketConfig类,代码如下:
```
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS();
}
}
```
10. 测试:启动Spring Boot后端,然后在微信开发者工具中点击编译并运行,即可在小程序中看到Spring Boot后端传来的消息。
阅读全文