Nashorn允许我们通过print()函数写入引擎使用的编写器。有没有办法使用内置功能读取阅读器?
例如:
Socket input=new Socket("stackoverflow.com", 1234);
Socket output=new Socket("stackoverflow.com", 4321);
engine.getContext().setWriter(new InputStreamReader(input.getInputStream())); //Changing to Socket input's reader from default one, which should be System.in.
engine.getContext().setWriter(new OutputStreamWriter(s.getOutputStream())); //Changing from System.out to Socket output's output stream.
在这种情况下,如果我使用print(“hello”),Socket输出将接收消息。但是如何读取Socket输入发送的内容?
答案 0 :(得分:0)
在ScriptContext上使用setReader和setWriter时,您将为Javascript上下文设置默认输入和输出流。所以,你在这里做的是配置Javascript来读取和写入套接字流。
如何使这些流可用于Javascript代码取决于您使用的JS环境/库。但是,使用Node.js,您可以将其视为process.stdin
和process.stdout
。
FWIW,标准ECMAScript库不支持访问stdin / stdout。