Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
leelance authored Jun 21, 2017
1 parent 24566e4 commit 79e5631
Showing 1 changed file with 27 additions and 47 deletions.
74 changes: 27 additions & 47 deletions spring-boot-websocket-netty-server/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# spring-boot-websocket-client, 依赖spring-boot-parent
# spring-boot-websocket-netty-server, 依赖spring-boot-parent
Spring Boot: user notifications with web socket

This example will shows how to send notifications, via web socket, to specific logged-in users.
Expand All @@ -9,60 +9,40 @@ Could be useful, for example, if you are trying to implement a real-time user no

#### Configurations
```java
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{
@SpringBootApplication
public class NettyWebSocketServerApplication implements CommandLineRunner{
@Autowired
private ChatServer chatServer;

public static void main(String[] args) {
SpringApplication.run(NettyWebSocketServerApplication.class, args);
}

@Bean
public ChatServer chatServer() {
return new ChatServer();
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws_notice").withSockJS();
public void run(String... args) throws Exception {
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 9090);
ChannelFuture future = chatServer.start(address);

Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run() {
chatServer.destroy();
}
});

future.channel().closeFuture().syncUninterruptibly();
}
}

@ResponseBody
@RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
public void sendMessage() {
List<MessageInfo> messages = getMessage();
messagingTemplate.convertAndSend("/user/topic/message", messages);
}
```

```js
<script>
function connect() {
var socket = new SockJS('/ws_notice');
var stompClient = Stomp.over(socket);

stompClient.connect({}, function(frame) {
stompClient.subscribe('/user/topic/message', function(message) {
notify(message.body);
});
});

return;
}

function notify(message) {
var html = "";
$.each(eval(message), function(index, val){
html = html
+ "<tr>"
+"<td>"+val.id+"</td>"
+"<td>"+val.name+"</td>"
+ "</tr>"
});

$('#tab').append(html);
}

$(document).ready(function() {
connect();
});
</script>
```
#### Prerequisites

- Java 7
- Java 8
- Maven > 3.0

#### From terminal
Expand Down

0 comments on commit 79e5631

Please sign in to comment.