欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java : mozilla rhino js Test URL or Socket

程序员文章站 2022-04-26 12:26:51
...
https://mozilla.github.io/rhino/ 下载 rhino1_7R5.zip ,解压后运行 cmd
cd D:\rhino\rhino1_7R5
编写测试脚本 test2.js
// Import the Swing GUI components and a few other classes
var swingNames = new JavaImporter(javax.swing, javax.swing.event, javax.swing.border, java.awt,java.awt.event); 

importPackage(java.net);
importPackage(java.io);
importPackage(java.util);
importClass(java.lang.Thread);

with (swingNames) {
var font = new Font("宋体",Font.PLAIN,12);
var frame = new JFrame("Test URL or Socket "); 
//frame.setSize(600,400);
frame.setLocation(200,200);
var txtfield = new JTextField(30);               // txt entry field
txtfield.setFont(font);
txtfield.setText("http://www.baidu.com/");
var button1 = new JButton("send");            // Button to send message
var button2= new JButton("http"); 
var filechooser = new JFileChooser();            // A file selection dialog
var row = Box.createHorizontalBox();             // A box for field and button
var col = Box.createVerticalBox();               // For the row & progress bars
var padding = new EmptyBorder(3,3,3,3);      // Padding for rows
var texta = new JTextArea(10,30);
texta.setEditable(false);
texta.setLineWrap(true);
var sp = new JScrollPane(texta);
//sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
// Put them all together and display the GUIm
row.add(txtfield);                               // Input field goes in the row
row.add(button1);                                 // Button goes in the row
row.add(button2); 
col.add(row);                                    // Row goes in the column
col.add(sp);
frame.add(col);                                  // Column goes in the frame
row.setBorder(padding);                      // Add some padding to the row
frame.pack();                                    // Set to minimum size
frame.visible = true;                            // Make the window visible
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// KeyEvent: ENTER
txtfield.addActionListener(function() {
    var txt = txtfield.getText();
    texta.append(txt+"\n");
    txtfield.setText("");
});
// Test Socket
button1.addActionListener(function() {
    texta.setText(""); // Clear
    var txt = txtfield.getText();
    if (! txt.startsWith("http")){
        texta.append(txt+"\n");
        new java.lang.Thread(function(){ send("127.0.0.1",12345,txt);}).start();
        txtfield.setText("");
    } else {
        texta.append("this is http, can't use socket.\n");
    }
});
// Test URL http:
button2.addActionListener(function() {
    texta.setText(""); // Clear
    var txt = txtfield.text;
    if (txt.startsWith("http:") || txt.startsWith("https:")){ 
        try {
            var url = new URL(txt.trim());
            new java.lang.Thread(function() { connect(url);}).start();
        } catch(e){
            texta.append(e.message);
        }
    } else {
        texta.append("input is not http://");
    }
});
// 连接
function connect(url){
	var conn = url.openConnection();
    conn.setConnectTimeout(5000);
	conn.connect();
	var input = new Scanner(conn.getInputStream(),"UTF-8");
	var headers = conn.getHeaderFields();
    print(typeof headers);
    print(Array.isArray(headers.entrySet()));
    var str = headers.entrySet().toString();
    texta.append(str.replace("], ","],\n")+"\n");
/*
	while (input.hasNextLine()) {
		texta.append(input.nextLine()+"\n");
    }
*/
}
// Socket
function send(host,port,msg){
    try {
        var socket = new java.net.Socket(host,port);
        var message = msg.trim();
        var writer = new java.io.PrintWriter(socket.getOutputStream(),true);
        writer.println(message);
        try {
            var ins = new java.util.Scanner(socket.getInputStream());
            while( ins.hasNextLine()){
                texta.append(ins.nextLine()+"\n");
            }
        } finally {
            socket.close();
        }
    } catch(ex){
        texta.append(ex.message);
    }
}
}

运行 java -jar js.jar test2.js
或者 rhino.bat test2.js