使用WebSocket模組
- 匯入
spring-boot-starter-websocket
依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
複製程式碼
package cn.java2016.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfiguration {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
複製程式碼
package cn.java2016.demo.component;
import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.springframework.stereotype.Component;
@ServerEndpoint("/websocket")
@Component
public class WebSocketServer {
@OnOpen
public void open(Session session) throws IOException {
System.out.println("connect..");
session.getBasicRemote().sendText("server: 登陸成功!");
}
@OnClose
public void close() {
System.out.println("disconnect..");
}
@OnMessage
public void message(String message, Session session) {
System.out.println("client send: " + message);
}
}
複製程式碼
if ('WebSocket' in window) {
var socket = new WebSocket("ws://localhost:8080/websocket")
socket.onopen = function (ev) {
console.log(ev)
socket.send('sfsd')
}
socket.onclose = function (ev) {
console.log(ev)
}
socket.onerror = function (ev) {
console.log(ev)
}
socket.onmessage = function (ev) {
console.log(ev.data)
}
}
複製程式碼
使用SocketIO模組
<dependency>
<groupId>com.corundumstudio.socketio</groupId>
<artifactId>netty-socketio</artifactId>
<version>1.7.16</version>
</dependency>
複製程式碼
package cn.java2016.demo.config;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
@Configuration
public class SocketIOConfiguration implements CommandLineRunner{
@Bean
public SocketIOServer socketIOServer() {
com.corundumstudio.socketio.Configuration conf = new com.corundumstudio.socketio.Configuration();
conf.setHostname("localhost");
conf.setPort(8081);
return new SocketIOServer(conf);
}
@Bean
public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketIOServer) {
return new SpringAnnotationScanner(socketIOServer);
}
@Override
public void run(String... args) throws Exception {
socketIOServer().start();
}
}
複製程式碼
package cn.java2016.demo.component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import cn.java2016.demo.pojo.User;
@Component
public class SocketServer {
@Autowired
public SocketIOServer socketServer;
@OnConnect
public void connect(SocketIOClient client) {
System.out.println("socketio connect..");
client.sendEvent("data", "連線成功!");
}
@OnDisconnect
public void disconnect(SocketIOClient client) {
System.out.println("socketio disconnect..");
}
@OnEvent("login")
public void login(SocketIOClient client, String name) {
System.out.println("login.." + name);
client.sendEvent("data", "login success2..");
}
@OnEvent("register")
public void register(User user, SocketIOClient client) {
System.out.println("register..");
System.out.println(user);
client.sendEvent("data", new User(user.getName(), user.getAge()).toString());
}
}
複製程式碼
<script src="https://cdn.bootcss.com/socket.io/2.1.1/socket.io.dev.js"></script>
複製程式碼
var socket = io("ws://localhost:8081")
socket.on('connect', function () {
console.log('connection..')
})
socket.on('disconnect', function () {
console.log('connection close..')
})
socket.on('data', function (data) {
console.log(data)
})
socket.emit("register", {name: "張三", age: 18})
socket.emit('login')
複製程式碼
注:Spring Boot DevTools熱載入不會重啟socket服務,需要手動重啟