Last active
January 23, 2017 16:55
-
-
Save co3moz/ca9d661f198c5218424e to your computer and use it in GitHub Desktop.
Socket.IO with spring and auto route
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.co3moz.client; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Repository; | |
/** | |
* BroadcastMessage (21 January 2016 14:27) | |
*/ | |
@Repository | |
public class Broadcast { | |
@Autowired | |
private SocketIOServer socketIOServer; | |
public void sendMessage(String event, String message) { | |
getSocketIOServer().getServer().getBroadcastOperations().sendEvent(event, message); | |
} | |
public void sendMessage(String event, Object object) { | |
getSocketIOServer().getServer().getBroadcastOperations().sendEvent(event, object); | |
} | |
public SocketIOServer getSocketIOServer() { | |
return socketIOServer; | |
} | |
public void setSocketIOServer(SocketIOServer socketIOServer) { | |
this.socketIOServer = socketIOServer; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this isn't in package "com.co3moz.client", it is in package "com.co3moz.client.events" | |
package com.co3moz.client.events; | |
import com.corundumstudio.socketio.AckRequest; | |
import com.corundumstudio.socketio.SocketIOClient; | |
import com.corundumstudio.socketio.listener.DataListener; | |
import com.co3moz.client.Broadcast; | |
import com.co3moz.client.GALCListener; | |
import com.co3moz.client.SocketIORoute; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Component; | |
/** | |
* Hail Event Example (22 January 2016 13:16) | |
*/ | |
@Component | |
@SocketIORoute | |
public class Hail implements DataListener<String> { | |
@Autowired | |
GALCListener galcListener; | |
public void onData(SocketIOClient client, String data, AckRequest ackRequest) { | |
client.sendEvent("hailResponse", "hail you :)"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<dependency> | |
<groupId>com.corundumstudio.socketio</groupId> | |
<artifactId>netty-socketio</artifactId> | |
<version>1.7.8</version> | |
</dependency> | |
<dependency> | |
<groupId>org.reflections</groupId> | |
<artifactId>reflections</artifactId> | |
<version>0.9.9-RC1</version> | |
</dependency> | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.co3moz.client; | |
import org.springframework.stereotype.Component; | |
import java.lang.annotation.*; | |
/** | |
* SocketIORoute (21 January 2016 14:36) | |
*/ | |
@Target(ElementType.TYPE) | |
@Retention(RetentionPolicy.RUNTIME) | |
@Documented | |
@Component | |
public @interface SocketIORoute { | |
String event() default "?class?"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.co3moz.client; | |
import com.corundumstudio.socketio.Configuration; | |
import org.apache.log4j.Logger; | |
import org.reflections.Reflections; | |
import org.reflections.scanners.TypeAnnotationsScanner; | |
import org.reflections.util.ClasspathHelper; | |
import org.reflections.util.ConfigurationBuilder; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.stereotype.Repository; | |
import javax.annotation.PostConstruct; | |
import javax.annotation.PreDestroy; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.ParameterizedType; | |
import java.lang.reflect.Type; | |
/** | |
* SocketIOServer (21 January 2016 14:23) | |
*/ | |
@Repository | |
public class SocketIOServer { | |
private final static Logger logger = Logger.getLogger(SocketIOServer.class); | |
private Configuration config; | |
private com.corundumstudio.socketio.SocketIOServer server; | |
@Autowired | |
ApplicationContext applicationContext; | |
@Value("${socketIO.hostName}") | |
String hostName; | |
@Value("${socketIO.port}") | |
int port; | |
@PostConstruct | |
public void initializeSocketIOServer() { | |
setConfig(new Configuration()); | |
getConfig().setHostname(hostName); | |
getConfig().setPort(port); | |
setServer(new com.corundumstudio.socketio.SocketIOServer(getConfig())); | |
Method method = com.corundumstudio.socketio.SocketIOServer.class.getDeclaredMethods()[0]; | |
for(Method m : com.corundumstudio.socketio.SocketIOServer.class.getDeclaredMethods()) { | |
if(m.getName().equals("addEventListener")) { | |
method = m; | |
} | |
} | |
System.out.println("Socket.IO starting to search beans"); | |
Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage("com.co3moz.events")).setScanners(new TypeAnnotationsScanner())); | |
for (Class<?> clazz : reflections.getTypesAnnotatedWith(SocketIORoute.class)) { | |
String event = clazz.getAnnotation(SocketIORoute.class).event(); | |
if(event.equals("?class?")) { | |
event = clazz.getSimpleName().toLowerCase(); | |
event = event.substring(0, 1).toLowerCase() + event.substring(1); | |
} | |
Type superClass = ((ParameterizedType) clazz.getGenericInterfaces()[0]).getActualTypeArguments()[0]; | |
try { | |
method.invoke(server, event, superClass, applicationContext.getBean(clazz)); | |
System.out.println("Socket.IO event:'" + event + "' initialized"); | |
} catch (Exception e) { | |
System.out.println("Socket.IO can't create event listeners. " + e.getMessage()); | |
} | |
} | |
server.start(); | |
System.out.println("Socket.IO server started at " + hostName + ":" + port); | |
} | |
@PreDestroy | |
public void shutdownSocketIOServer() { | |
server.stop(); | |
} | |
public Configuration getConfig() { | |
return config; | |
} | |
public void setConfig(Configuration config) { | |
this.config = config; | |
} | |
public com.corundumstudio.socketio.SocketIOServer getServer() { | |
return server; | |
} | |
public void setServer(com.corundumstudio.socketio.SocketIOServer server) { | |
this.server = server; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment