Skip to content

sociablesasha/OnlineChat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PickUp SpringBoot REST API Facebook

PickUp

Purpose

  1. Understand WebSocket.
  2. ... MessageBroker
  3. ... SockJS
  4. ... STOMP

Technical Stack

  • Spring Boot(Maven)
  • WebSocket
  • MessageBroker
  • SockJS
  • STOMP

Spring Boot

HttpRequest -> Controller -> View

Applicatin.java

Main Method in Spring Boot.

public class Application {

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

}

WebSocketConfig.java

WebSocketMessageBroker.

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/receive");
        config.setApplicationDestinationPrefixes("/send");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/websocket").withSockJS();
    }

}

TalkController.java

Talk Controller

@Controller
public class TalkController {

    // Member
    static ArrayList<String> members = new ArrayList<>();

    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @MessageMapping("/in")
    @SendTo("/receive/member")
    public ArrayList<String> in (Talk talk) {
        members.add(talk.getName());
        return members;
    }

    @MessageMapping("/out")
    @SendTo("/receive/member")
    public ArrayList<String> out (Talk talk) {
        members.remove(talk.getName());
        return members;
    }

    @MessageMapping("/chats")
    @SendTo("/receive/chats")
    public String talk(Talk talk) {
        return talk.getName() + " : " + talk.getContent();
    }

}

Photo

Main

2018-09-19 3 50 30

2018-09-19 3 48 44

Histories

2018.09.18. ~ 2018.09.19. WebSocket MessageBroker