个人收款码 CPU iphone wpf forms graph cmd outlook vue异步加载组件 网站后台管理模板 easyui视频 electron教程 bootstrap日历插件 div外边距 android逆向工程师 pr序列设置哪个好 mysql自连接 websocket库 磁盘清理会误删东西吗 java高级特性 python中的index python的开发工具 java抽象类 javaif语句 java语言 学java基础 怎么装linux系统 html实例教程 acmecadconverter 图片放大软件 ad下载 qq免安装 在线手册 god2iso spss20安装教程 天正建筑2007 小米9截图 linux解压命令 安卓游戏辅助 igfxtray
当前位置: 首页 > 学习教程  > 

Java WebSocket库:https://github.com/TooTallNate/Java-WebSocket

2020/10/16 17:48:12 文章标签: websocket库

https://github.com/TooTallNate/Java-WebSocket 以下是简单示例: import com.google.gson.JsonObject; import com.google.gson.JsonParser; import static java.lang.System.err; import static java.lang.System.out; import java.net.InetSocketAddress; import…

https://github.com/TooTallNate/Java-WebSocket

以下是简单示例:

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import static java.lang.System.err;
import static java.lang.System.out;
import java.net.InetSocketAddress;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

public class Main {

    private static class WebScoketServerImpl extends WebSocketServer {

        private WebScoketServerImpl(int port) {
            super(new InetSocketAddress(port));
        }

        @Override
        public void onOpen(WebSocket conn, ClientHandshake handshake) {
            out.println("WebScoketServerImpl.onOpen(WebSocket, ClientHandshake) called");
        }

        @Override
        public void onClose(WebSocket conn, int code, String reason, boolean remote) {
            out.println("WebScoketServerImpl.onClose(WebSocket, int, String, boolean) called: " + reason);
        }

        @Override
        public void onMessage(WebSocket conn, String message) {
            out.println("WebScoketServerImpl.onMessage(WebSocket, String) called: " + message);
            JsonObject root = new JsonParser().parse(message).getAsJsonObject();
            if (root.get("type").getAsString().equals("print")) {
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("type", "print");
                jsonObject.addProperty("error_message", "None");
                jsonObject.addProperty("error_code", 1);
                conn.send(jsonObject.toString());
            } else {
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("type", "getWeight");
                jsonObject.addProperty("error_code", 0);
                jsonObject.addProperty("weight", "0.000kg");
                conn.send(jsonObject.toString());
            }
        }

        @Override
        public void onError(WebSocket conn, Exception ex) {
            err.println("WebScoketServerImpl.onError(WebSocket, Exception) called: " + ex);
        }

        @Override
        public void onStart() {
            out.println("WebScoketServerImpl.onStart() called");
        }

    }

    public static void main(String[] args) {
        Main.WebScoketServerImpl websocketServer = new Main.WebScoketServerImpl(1234);
        websocketServer.start();
    }
}

 

转载于:https://www.cnblogs.com/buyishi/p/8920453.html


本文链接: http://www.dtmao.cc/news_show_300155.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?