Skip to content

Instantly share code, notes, and snippets.

@jrgleason
Created June 30, 2014 19:12
Show Gist options
  • Save jrgleason/56ae5ca2b8134d5274d1 to your computer and use it in GitHub Desktop.
Save jrgleason/56ae5ca2b8134d5274d1 to your computer and use it in GitHub Desktop.
Example of Spring4 Websocket
package com.gleason.itext;
import java.util.Arrays;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PathVariable;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
private static Class<Application> applicationClass = Application.class;
}
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.4.BUILD-SNAPSHOT")
}
}
apply plugin: 'war'
apply plugin: 'spring-boot'
apply plugin: 'groovy'
war { baseName='itext' }
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
maven { url "http://repo.spring.io/libs-release" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-websocket")
compile("org.springframework:spring-messaging")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
testCompile("org.springframework.boot:spring-boot-starter-test")
compile 'org.codehaus.groovy:groovy-all:2.2.0'
compile 'com.lowagie:itext:4.2.1'
compile 'com.google.code.gson:gson:2.2.4'
}
package com.gleason.itext.model;
/**
* Created with IntelliJ IDEA.
* User: jgleason
* Date: 6/30/14
* Time: 1:30 PM
* To change this template use File | Settings | File Templates.
*/
public class HelloMessage {
private String name;
public String getName() {
return name;
}
}
package com.gleason.itext.websocket;
import com.gleason.itext.model.HelloMessage;
import com.google.gson.Gson;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.handler.TextWebSocketHandler;
/**
* Created with IntelliJ IDEA.
* User: jgleason
* Date: 6/30/14
* Time: 1:18 PM
* To change this template use File | Settings | File Templates.
*/
public class MyHandler extends TextWebSocketHandler {
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
System.out.println("Testing "+message.getPayload());
Gson gson = new Gson();
HelloMessage helloMessage = gson.fromJson(message.getPayload(), HelloMessage.class);
System.out.println(helloMessage.getName());
}
}
package com.gleason.itext;
import com.gleason.itext.websocket.MyHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.WebSocketHandler;
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(myHandler(), "/myHandler");
}
@Bean
public WebSocketHandler myHandler() {
return new MyHandler();
}
}
@filip505
Copy link

Nice and simple, thank you 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment