< project xmlns = " http : / / maven . apache . org / POM / 4 . 0 . 0 " xmlns : xsi = " http : / / www . w 3 . org / 2001 / XMLSchema - instance " xsi : schemaLocation = " http : / / maven . apache . org / POM / 4 . 0 . 0 http : / / maven . apache . org / xsd / maven - 4 . 0 . 0 . xsd " > < modelVersion > 4 . 0 . 0 < / modelVersion > < parent > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - starter - parent < / artifactId > < version > 2 . 1 . 3 . RELEASE < / version > < relativePath / > < ! - - lookup parent from repository - - > < / parent > < groupId > com . oy < / groupId > < artifactId > netty socketio 007 < / artifactId > < version > 0 . 0 . 1 < / version > < name > netty socketio 007 < / name > < description > netty socketio 007 for Spring Boot < / description > < properties > < java . version > 1 . 8 < / java . version > < / properties > < dependencies > < dependency > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - starter < / artifactId > < / dependency > < dependency > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - starter - tomcat < / artifactId > < scope > provided < / scope > < / dependency > < dependency > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - starter - test < / artifactId > < scope > test < / scope > < / dependency > < dependency > < groupId > com . corundumstudio . socketio < / groupId > < artifactId > netty - socketio < / artifactId > < version > 1 . 7 . 11 < / version > < / dependency > < dependency > < groupId > com . alibaba < / groupId > < artifactId > fastjson < / artifactId > < version > 1 . 2 . 47 < / version > < / dependency > < / dependencies > < build > < plugins > < plugin > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - maven - plugin < / artifactId > < / plugin > < / plugins > < / build > < / project > package com . oy ; import org . springframework . boot . Com mandLineRunner ; import org . springframework . boot . SpringApplication ; import org . springframework . boot . autoconfigure . SpringBootApplication ; import org . springframework . context . annotation . Bean ; import org . springframework . core . annotation . Order ; import com . corundumstudio . socketio . Configuration ; import com . corundumstudio . socketio . SocketIOServer ; import com . corundumstudio . socketio . annotation . SpringAnnotationScanner ; @ SpringBootApplication @ Order ( 1 ) public class Netty socketio 007 Application implements CommandLineRunner { private SocketIOServer server ; public static void main ( String [ ] args ) { SpringApplication . run ( Netty socketio 007 Application . class , args ) ; } @ Bean public SocketIOServer socketIOServer ( ) { Configuration config = new Configuration ( ) ; config . setHostname ( " localhost " ) ; config . setPort ( 4001 ) ; this . server = new SocketIOServer ( config ) ; return server ; } @ Bean public SpringAnnotationScanner springAnnotationScanner ( SocketIOServer socketServer ) { return new SpringAnnotationScanner ( socketServer ) ; } @ Override public void run ( String . . . args ) throws Exception { server . start ( ) ; UtilFunctions . log . info ( " socket . io run success ! " ) ; / / 向 " channel _ 1 " push 数据 Service . send ( args ) ; } } package com . oy ; import java . util . Set ; import java . util . UUID ; import org . springframework . beans . factory . annotation . Autowired ; import org . springframework . stereotype . Com ponent ; import com . corundumstudio . socketio . AckRequest ; import com . corundumstudio . socketio . SocketIOClient ; import com . corundumstudio . socketio . SocketIOServer ; import com . corundumstudio . socketio . annotation . OnConnect ; import com . corundumstudio . socketio . annotation . OnDisconnect ; import com . corundumstudio . socketio . annotation . OnEvent ; @ Component public class MessageEventHandler { public static SocketIOServer socketIoServer ; @ Autowired public MessageEventHandler ( SocketIOServer server ) { MessageEventHandler . socketIoServer = server ; } @ OnConnect public void onConnect ( SocketIOClient client ) { UUID socketSessionId = client . getSessionId ( ) ; String ip = client . getRemoteAddress ( ) . toString ( ) ; UtilFunctions . log . info ( " client connect , socketSessionId : { } , ip : { } " , socketSessionId , ip ) ; } @ OnEvent ( " sub " ) public void sub ( SocketIOClient client , AckRequest request , String channel ) { UUID socketSessionId = client . getSessionId ( ) ; String ip = client . getRemoteAddress ( ) . toString ( ) ; client . joinRoom ( channel ) ; UtilFunctions . log . info ( " client sub , channel : { } , socketSessionId : { } , ip : { } " , channel , socketSessionId , ip ) ; Set < String > rooms = client . getAllRooms ( ) ; for ( String room : rooms ) { UtilFunctions . log . info ( " after client connect , room : { } " , room ) ; } / / 客户 端 一 订阅 , 就 马上 push 一次 sendAllEvent ( Service . getMsg ( ) ) ; } / / @ OnEvent ( " unsub " ) / / public void unsub ( SocketIOClient client , AckRequest request , String channel ) { / / UUID socketSessionId = client . getSessionId ( ) ; / / String ip = client . getRemoteAddress ( ) . toString ( ) ; / / client . leaveRoom ( channel ) ; / / UtilFunctions . log . info ( " client unsub , channel : { } , socketSessionId : { } , ip : { } " , channel , socketSessionId , ip ) ; / / } @ OnDisconnect public void onDisconnect ( SocketIOClient client ) { UUID socketSessionId = client . getSessionId ( ) ; String ip = client . getRemoteAddress ( ) . toString ( ) ; UtilFunctions . log . info ( " client disconnect , socketSessionId : { } , ip : { } " , socketSessionId , ip ) ; Set < String > rooms = client . getAllRooms ( ) ; for ( String room : rooms ) { UtilFunctions . log . info ( " after client disconnect , room : { } " , room ) ; } } / / broadcast to channel " channel _ 1 " public static void sendAllEvent ( String data ) { socketIoServer . getRoomOperations ( " channel _ 1 " ) . sendEvent ( " channel _ 1 " , data ) ; } } package com . oy ; import com . alibaba . fastjson . JSONObject ; public class Service { private static String msg ; / / 向 " channel _ 1 " push 数据 public static void send ( String [ ] args ) throws Exception { int price = 0 ; if ( args ! = null & & args . length > 0 ) { try { price = Integer . parseInt ( args [ 0 ] ) ; } catch ( Exception e ) { UtilFunctions . log . info ( " args [ 0 ] 不 能 转换 为 int " ) ; new Exception ( e ) ; } } for ( int i = 0 ; i < 1000 ; i + + ) { JSONObject data = new JSONObject ( ) ; data . put ( " current _ price " , price + + ) ; Service . msg = data . toJSONString ( ) ; / / 把 每次 push 的 数据 保存 起来 MessageEventHandler . sendAllEvent ( data . toJSONString ( ) ) ; Thread . sleep ( 1000 * 5 ) ; } } public static String getMsg ( ) { return msg ; } public static void setMsg ( String msg ) { Service . msg = msg ; } } < ! DOCTYPE html PUBLIC " - / / W 3C / / DTD HTML 4 . 01 Transitional / / EN " " http : / / www . w 3 . org / TR / html 4 / loose . dtd " > < html > < head > < meta http - equiv = " Content - Type " content = " text / html ; charset = UTF - 8 " > < title > Insert title here < / title > < script src = " http : / / code . jquery . com / jquery - 1 . 12 . 4 . min . js " > < / script > < script type = " text / javascript " src = " socket . io . js " > < / script > < style > body { padding : 20 px ; } # console { overflow : auto ; } . username - msg { color : orange ; } . connect - msg { color : green ; } . disconnect - msg { color : red ; } . send - msg { color : # 888 } < / style > < / head > < body > < div id = " console " class = " well " > < / div > < br / > < br / > current _ price : < span id = " current _ price " > < / span > < br / > < / body > < script type = " text / javascript " > var socket = io . connect ( ' http : / / localhost : 4001 ' , { transports : [ ' websocket ' ] } ) ; socket . on ( ' connect ' , function ( ) { console . log ( " msg 页面 连接 成功 ! " ) ; socket . emit ( ' sub ' , " channel _ 1 " ) ; output ( ' < span class = " connect - msg " > Client has connected to the server ! < / span > ' ) ; output ( ' < span class = " connect - msg " > Client send { " event " : " sub " , " channel " : " channel _ 1 " } < / span > ' ) ; } ) ; socket . on ( ' disconnect ' , function ( ) { output ( ' < span class = " disconnect - msg " > The client has disconnected ! < / span > ' ) ; } ) ; socket . on ( ' channel _ 1 ' , function ( data ) { var jsonObj = eval ( " ( " + data + " ) " ) ; console . log ( " 收到 cfd _ md 的 消息 : " + data ) ; $ ( " # current _ price " ) . html ( jsonObj . current _ price ) ; } ) ; function output ( message ) { var currentTime = " < span class = ' time ' > " + NowTime ( ) + " < / span > " ; var element = $ ( " < div > " + currentTime + " " + message + " < / div > " ) ; $ ( ' # console ' ) . prepend ( element ) ; } / / 获取 当前 时间 function NowTime ( ) { var time = new Date ( ) ; var year = time . getFullYear ( ) ; / / 获取 年 var month = time . getMonth ( ) + 1 ; / / 或者 月 var day = time . getDate ( ) ; / / 或者 天 var hour = time . getHours ( ) ; / / 获取 小时 var minu = time . getMinutes ( ) ; / / 获取 分钟 var second = time . getSeconds ( ) ; / / 或者 秒 var data = year + " - " ; if ( month < 10 ) { data + = " 0 " ; } data + = month + " - " ; if ( day < 10 ) { data + = " 0 " } data + = day + " " ; if ( hour < 10 ) { data + = " 0 " } data + = hour + " : " ; if ( minu < 10 ) { data + = " 0 " } data + = minu + " : " ; if ( second < 10 ) { data + = " 0 " } data + = second ; return data ; } < / script > < / html >" />
您当前的位置:首页 > 博客教程

socketio spring boot

时间:2023-11-20 22:14 阅读数:1693人阅读

?0? Non-compliance ICP Filing

≥ω≤

˙^˙

∩0∩

evo加速器部分文章、数据、图片来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知删除。邮箱:xxxxxxx@qq.com