websocket浅谈

1、怎么搭建一个简易的websocket架子,让前端后端用来交互?

我们采用引入三方jar的方式进行搭建,需要的jar如下:

复制代码
       <dependency>
            <groupId>org.atmosphere</groupId>
            <artifactId>atmosphere-runtime</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
        </dependency>

需要再web,xml里面进行配置一个servlet

复制代码
    <servlet>
        <description>AtmosphereServlet</description>
        <servlet-name>AtmosphereServlet</servlet-name>
        <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
        <init-param>
            <param-name>org.atmosphere.cpr.packages</param-name>
            <param-value>具体的包的目录</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>AtmosphereServlet</servlet-name>
        <url-pattern>/websocket/*</url-pattern>
    </servlet-mapping>

前端需要引入 atmosphere.js

前端可以封装一个简易的调用

复制代码
html:

<input id="send" type="input">
<input type="button" onclick="send()" value="发送">
<span id="sendvalue"></span>




js:

var request={
            url:'http://localhost:8082/工程名称/websocket/test',
            contentType:"application/json",
            transport:"websocket",
            fallbackTransport:"long-polling",
            onTransportFailure: function(errorMsg, request) {
                request.fallbackTransport = "long-polling";
                transport = "long-polling";
            },
            onMessage:function(response) {
                var msgStr = response.responseBody;
                $("#sendvalue").text(msgStr);
                }


        }
        var subSocket=atmosphere.subscribe(request);


        function send(){
            var send=$('#send').val();
            subSocket.push(send);
        }

后端接收代码

复制代码
import lombok.extern.slf4j.Slf4j;
import org.atmosphere.config.service.Disconnect;
import org.atmosphere.config.service.ManagedService;
import org.atmosphere.config.service.Message;
import org.atmosphere.config.service.Ready;
import org.atmosphere.cpr.Broadcaster;

import javax.inject.Inject;
import javax.inject.Named;

@Slf4j
@ManagedService(path = "websocket/test")
public class WebsocketTest {


    @Inject
    @Named("websocket/test")
    private static Broadcaster broadcaster;

    @Ready
    public void onReady() {
        log.info("onReady"); //准备
    }

    @Disconnect
    public void onDisconnect() {
        log.info("onDisconnect"); //销毁
    }

    @Message
    public void onMessage(String message) {
        log.info("onMessage");
        broadcaster.broadcast(message);  //发送给前端
    }
}
相关推荐
杨了个杨89821 小时前
memcached部署
qt·websocket·memcached
aesthetician9 小时前
实时通信的艺术:Server-Sent Events (SSE) 与 WebSocket 的深度解析
网络·websocket·网络协议
CaracalTiger10 小时前
OpenClaw-VSCode:在 VS Code 中通过 WebSocket 远程管理 OpenClaw 网关的完整方案
运维·ide·人工智能·vscode·websocket·开源·编辑器
默默前行的虫虫1 天前
解决EMQX WebSocket连接不稳定及优化WS配置提升稳定性?
websocket
闲人编程1 天前
使用FastAPI和WebSocket构建高性能实时聊天系统
websocket·网络协议·网络编程·fastapi·持久化·实时聊天·codecapsule
青春给了代码2 天前
基于WebSocket实现在线语音(实时+保存)+文字双向传输完整实现
网络·websocket·网络协议
我爱加班、、2 天前
Websocket能携带token过去后端吗
前端·后端·websocket
“负拾捌”2 天前
python + uniapp 结合腾讯云实现实时语音识别功能(WebSocket)
python·websocket·微信小程序·uni-app·大模型·腾讯云·语音识别
Trouvaille ~3 天前
【Linux】UDP Socket编程实战(一):Echo Server从零到一
linux·运维·服务器·网络·c++·websocket·udp
lang201509283 天前
Java WebSocket API:JSR-356详解
java·python·websocket