最新消息:

springboot websocket

java开发 luther 495浏览 0评论

pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

配置:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}

}

服务端:

package com.demo.websket;

import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.springframework.stereotype.Component;

@ServerEndpoint("/realtime/program")
@Component
public class Ws{

private static final Set<Ws> connections = new CopyOnWriteArraySet<>();
private Session session;

@OnOpen
public void start(Session session) {
this.session = session;
connections.add(this);
String message = String.format("%s %s", session.getId(), "has joined.");
broadcast(message);
}

@OnClose
public void end() {
connections.remove(this);
String message = String.format("%s %s", session.getId(), "has disconnected.");
broadcast(message);
}

@OnMessage
public void incoming(String message) {
String msg = String.format("%s %s %s", session.getId(), "said:", message);
broadcast(msg);
}

@OnError
public void onError(Throwable t) throws Throwable {
}

private static void broadcast(String msg) {
for (Ws client : connections) {
try {
synchronized (client) {
client.session.getBasicRemote().sendText(msg);
}
} catch (IOException e) {
connections.remove(client);
try {
client.session.close();
} catch (IOException e1) {
}
String message = String.format("%s %s", client.session.getId(), "has been disconnected.");
broadcast(message);
}
}
}

}

nginx 配置

location /realtime/program { #没有/斜杠结尾
proxy_pass http://127.0.0.1:38080/realtime/program;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection “Upgrade”;
}

客户端js:

ChatInit(){
letChat=this.Chat;
Chat.socket=null;
Chat.connect= (function(host) {
if (‘WebSocket’inwindow) {
Chat.socket=newWebSocket(host);
} else if (‘MozWebSocket’ in window) {
Chat.socket=newMozWebSocket(host);
} else {
console.log(‘Error: WebSocket is not supported by this browser.’);
return;
}
Chat.socket.onopen=function () {
console.log(‘Info: WebSocket connection opened.’);
this.disableSend=true;
// document.getElementById(‘chat’).onkeydown = function(event) {
// if (event.keyCode == 13) {
// Chat.sendMessage();
// }
// };
};
Chat.socket.onclose=function () {
this.disableSend=true;
console.log(‘Info: WebSocket closed.’);
};
Chat.socket.onmessage=function (message) {
console.log(message.data);
};
});
Chat.initialize=function() {
leturl=window.location.host + ‘/h5srv/realtime/program’;
if (window.location.protocol==’http:’) {
Chat.connect(‘ws://’+url );
} else {
Chat.connect(‘wss://’+url);
}
};
Chat.sendMessage= (function() {
letmessage=this.message;
if (message!=”) {
Chat.socket.send(message);
document.getElementById(‘chat’).value=”;
}
});
Chat.initialize();
console.log(Chat);
}

 

 

 

转载请注明:应用享评 » springboot websocket

您必须 登录 才能发表评论!