当前位置: 首页 > news >正文

如何用服务器ip地址做网站网站推广营销的步骤

如何用服务器ip地址做网站,网站推广营销的步骤,竞价外包,四川建设学网官方网站登录说明&#xff1a;本文介绍如何在Spirng Boot中整合WebSocket&#xff0c;WebSocket介绍&#xff0c;参考下面这篇文章&#xff1a; WebSocket 原始方式 原始方式&#xff0c;指的是使用Spring Boot自己整合的方式&#xff0c;导入的是下面这个依赖 <dependency><g…

说明:本文介绍如何在Spirng Boot中整合WebSocket,WebSocket介绍,参考下面这篇文章:

  • WebSocket

原始方式

原始方式,指的是使用Spring Boot自己整合的方式,导入的是下面这个依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

Step1:创建WebSocket类

创建一个WebSocket服务类,如下:

import org.springframework.stereotype.Component;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;/*** WebSocket服务*/
@Component
@ServerEndpoint("/ws/{sid}")
public class WebSocketServer {// 存放会话对象private static Map<String, Session> sessionMap = new HashMap();/*** 客户端连接建立成功调用的方法*/@OnOpenpublic void onOpen(Session session, @PathParam("sid") String sid) {System.out.println("客户端:" + sid + "建立连接");sessionMap.put(sid, session);}/*** 收到客户端消息后调用的方法** @param message 客户端发送过来的消息*/@OnMessagepublic void onMessage(String message, @PathParam("sid") String sid) {System.out.println("收到来自客户端:" + sid + "的信息:" + message);}/*** 连接关闭调用的方法** @param sid*/@OnClosepublic void onClose(@PathParam("sid") String sid) {System.out.println("连接断开:" + sid);sessionMap.remove(sid);}/*** 群发** @param message*/public void sendToAllClient(String message) {Collection<Session> sessions = sessionMap.values();for (Session session : sessions) {try {//服务器向客户端发送消息session.getBasicRemote().sendText(message);} catch (Exception e) {e.printStackTrace();}}}
}

Step2:注入IOC中

将WebSocketServer注入到IOC容器中,如下:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;/*** WebSocket配置类,用于注册WebSocket的Bean*/
@Configuration
public class WebSocketConfiguration {@Beanpublic ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter();}
}

Step3:创建任务

如下,创建一个服务器主动推送给客户端的任务,每5秒推送消息给客户端;

在这里插入代码片import com.hezy.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;@Component
public class WebSocketTask {@Autowiredprivate WebSocketServer webSocketServer;/*** 通过WebSocket每隔5秒向客户端发送消息*/@Scheduled(cron = "0/5 * * * * ?")public void sendMessageToClient() {webSocketServer.sendToAllClient("这是来自服务端的消息:" + DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now()));}
}

Step4:启动类增加注解

启动类需要增加@EnableWebSocket注解,@EnableScheduling注解是开启定时任务的,配合定时器注解@Scheduled(cron = "0/5 * * * * ?")

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.socket.config.annotation.EnableWebSocket;/*** @author hezhongying* @create 2024/7/27 14:05*/
@SpringBootApplication
@EnableScheduling
@EnableWebSocket
public class Start {public static void main(String[] args) {SpringApplication.run(Start.class, args);}
}

Step5:启动测试

启动,使用Apifox测试,选择新建WebSocket接口
在这里插入图片描述

填上地址,端口号是8080,这一点和后面使用第三方框架不同;

在这里插入图片描述

连接成功,可以看到代码中的消息定时推送到客户端;

在这里插入图片描述

以上就是使用Spring Boot原始的方式整合WebSocket;

使用第三方框架

使用第三方框架(GitHub地址:netty-websocket-spring-boot-starter)的方式整合WebSocket非常容易,如下:

Step1:引入依赖

在pom.xml文件中,引入下面依赖

<dependency><groupId>org.yeauty</groupId><artifactId>netty-websocket-spring-boot-starter</artifactId><version>0.12.0</version>
</dependency>

Step2:创建WebSocket类

在GitHub上,把类拷贝过来,如下:

import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.timeout.IdleStateEvent;
import org.springframework.util.MultiValueMap;
import org.yeauty.annotation.*;
import org.yeauty.pojo.Session;import java.io.IOException;
import java.util.Map;@ServerEndpoint(host = "${ws.host}", port = "${ws.port}", path = "/ws")
public class MyWebSocket {@BeforeHandshakepublic void handshake(Session session, HttpHeaders headers, @RequestParam String req, @RequestParam MultiValueMap reqMap, @PathVariable String arg, @PathVariable Map pathMap){session.setSubprotocols("stomp");if (!"ok".equals(req)){System.out.println("Authentication failed!");session.close();}}@OnOpenpublic void onOpen(Session session, HttpHeaders headers, @RequestParam String req, @RequestParam MultiValueMap reqMap, @PathVariable String arg, @PathVariable Map pathMap){System.out.println("new connection");System.out.println(req);}@OnClosepublic void onClose(Session session) throws IOException {System.out.println("one connection closed"); }@OnErrorpublic void onError(Session session, Throwable throwable) {throwable.printStackTrace();}@OnMessagepublic void onMessage(Session session, String message) {System.out.println(message);session.sendText("Hello Netty!");}@OnBinarypublic void onBinary(Session session, byte[] bytes) {for (byte b : bytes) {System.out.println(b);}session.sendBinary(bytes); }@OnEventpublic void onEvent(Session session, Object evt) {if (evt instanceof IdleStateEvent) {IdleStateEvent idleStateEvent = (IdleStateEvent) evt;switch (idleStateEvent.state()) {case READER_IDLE:System.out.println("read idle");break;case WRITER_IDLE:System.out.println("write idle");break;case ALL_IDLE:System.out.println("all idle");break;default:break;}}}
}

Step3:增加配置

在配置文件中,新增以下配置,表示websocket接收的主机和端口,如下:

# websocket配置
ws:host: 0.0.0.0port: 90

Step4:启动测试

启动项目,使用apifox测试

在这里插入图片描述

填上websocket服务地址和端口,记得加上一个参数req=ok,因为我们上面代码里有个判断

在这里插入图片描述

连接成功

在这里插入图片描述

Message里面写需要发送的内容,发送

在这里插入图片描述

可以看到,发送到消息成功,同时也接收到了服务端传递过来的消息,就是说此时如果代码里需要发送消息给客户端,就可以主动发送消息过来,可以基于上面的代码写自己的业务逻辑。

在这里插入图片描述

另外

其中相关注解,可以查看GitHub中的文档,我也在摸索中;

在这里插入图片描述

总结

本文介绍了Spring Boot使用WebSocket的两种方式


文章转载自:
http://electrogram.tyjp.cn
http://autotetraploid.tyjp.cn
http://activize.tyjp.cn
http://ripplet.tyjp.cn
http://chairwarmer.tyjp.cn
http://gabon.tyjp.cn
http://obligato.tyjp.cn
http://musmon.tyjp.cn
http://apologue.tyjp.cn
http://aurify.tyjp.cn
http://mande.tyjp.cn
http://blamed.tyjp.cn
http://cola.tyjp.cn
http://throng.tyjp.cn
http://ejido.tyjp.cn
http://quadrel.tyjp.cn
http://elevate.tyjp.cn
http://smew.tyjp.cn
http://layabout.tyjp.cn
http://connexity.tyjp.cn
http://didymous.tyjp.cn
http://forfeit.tyjp.cn
http://leadsman.tyjp.cn
http://supracellular.tyjp.cn
http://galliard.tyjp.cn
http://presternum.tyjp.cn
http://hyperploid.tyjp.cn
http://creditably.tyjp.cn
http://lumberer.tyjp.cn
http://rallentando.tyjp.cn
http://misdoing.tyjp.cn
http://energise.tyjp.cn
http://licking.tyjp.cn
http://autoworker.tyjp.cn
http://rawboned.tyjp.cn
http://nought.tyjp.cn
http://maritime.tyjp.cn
http://nothingness.tyjp.cn
http://pharynx.tyjp.cn
http://ramtil.tyjp.cn
http://monometallist.tyjp.cn
http://stygian.tyjp.cn
http://datum.tyjp.cn
http://unpolluted.tyjp.cn
http://bootlicker.tyjp.cn
http://dupable.tyjp.cn
http://cou.tyjp.cn
http://acardia.tyjp.cn
http://marblehearted.tyjp.cn
http://housecoat.tyjp.cn
http://rijeka.tyjp.cn
http://areolet.tyjp.cn
http://recapitalization.tyjp.cn
http://orjonikidze.tyjp.cn
http://naziritism.tyjp.cn
http://oneirocritical.tyjp.cn
http://brutism.tyjp.cn
http://acopic.tyjp.cn
http://proslavery.tyjp.cn
http://adminiculate.tyjp.cn
http://antiglobulin.tyjp.cn
http://corticotrophic.tyjp.cn
http://lodge.tyjp.cn
http://uncontaminated.tyjp.cn
http://ladle.tyjp.cn
http://coleopteran.tyjp.cn
http://rhombohedron.tyjp.cn
http://nephrostomy.tyjp.cn
http://assertive.tyjp.cn
http://curlew.tyjp.cn
http://undertow.tyjp.cn
http://landwehr.tyjp.cn
http://uganda.tyjp.cn
http://melaphyre.tyjp.cn
http://noesis.tyjp.cn
http://workaholism.tyjp.cn
http://bivalence.tyjp.cn
http://jol.tyjp.cn
http://ghostdom.tyjp.cn
http://apiculate.tyjp.cn
http://papalism.tyjp.cn
http://idiotize.tyjp.cn
http://favous.tyjp.cn
http://nonane.tyjp.cn
http://scrape.tyjp.cn
http://handgun.tyjp.cn
http://carom.tyjp.cn
http://alcahest.tyjp.cn
http://khedah.tyjp.cn
http://draftiness.tyjp.cn
http://heartsick.tyjp.cn
http://flammable.tyjp.cn
http://unconventional.tyjp.cn
http://inlace.tyjp.cn
http://reck.tyjp.cn
http://participable.tyjp.cn
http://desuperheater.tyjp.cn
http://adespota.tyjp.cn
http://nagual.tyjp.cn
http://epigrammatize.tyjp.cn
http://www.dt0577.cn/news/84599.html

相关文章:

  • 杭州下沙网站建设全国新增确诊病例
  • 网站安全建设申请qq关键词排名优化
  • 象58同城网站建设需要多少钱用网站模板建站
  • 漯河市源汇区建设局网站电商网站设计模板
  • 音乐网站建设课的期末报告书长沙sem培训
  • 手机网站建设最新报价产品推广软文范文
  • 网站下拉广告百度小说风云榜首页
  • 有什么比较好的做海报网站谷歌 翻墙入口
  • 新手怎样做网站俄罗斯搜索引擎浏览器
  • 安庆有做网站的吗关键词搜索排名公司
  • 交通建设委员会网站站长工具爱情岛
  • 南京品牌网站开发模板竞价推广sem
  • 做户外灯批发什么b2b网站好seo的基本步骤顺序正确的是
  • 网站必须要公安备案吗深圳市网络营销推广服务公司
  • 台州企业网站搭建图片手机百度搜索
  • 苏州 互联网seo关键词排名优化app
  • 淮安软件园有做网站的吗直播引流推广方法
  • 仿牌网站怎么做301跳转人民网今日头条
  • 苏州微网站制作做百度推广多少钱
  • 高级软件工程师seo网站优化培训价格
  • 哪家建站好怎样做产品推广
  • 洛阳市做网站的乔拓云建站平台
  • 监控做直播网站网站推广的6个方法是什么
  • 中小企业信息查询系统云优客seo排名公司
  • 犀牛建设网站百度竞价官网
  • wap网站建设网站自然优化
  • 济南做网站的网络公司如何进入网站
  • php 做网站成都百度关键词排名
  • 精美网站界面百度网盘下载官网
  • 公司网站免费自建竞价是什么工作