简体   繁体   English

Java服务器与javascript之间的通信

[英]communication between java server and javascript

I have multithreaded chatting java server which can handle number of clients (java). 我有一个多线程聊天Java服务器,它可以处理多个客户端(java)。 They can simultaneous talk with each other. 他们可以同时互相交谈。 They are connected through network socket. 它们通过网络插座连接。 Besides their own conversation words, my purpose is to display the conversation words they do in web browser through web application. 除了他们自己的对话单词之外,我的目的是通过Web应用程序显示他们在网络浏览器中所做的对话单词。 I am thinking about JavaScript but couldn't figure out how to implement javascript for web application because I will need object or data to pass to javascript side from server( java) side. 我正在考虑JavaScript,但无法弄清楚如何为Web应用程序实现javascript,因为我需要对象或数据才能从server(java)端传递到javascript端。

Following is the multithreaded server and this works fine with multiple clients. 以下是多线程服务器,它可以在多个客户端上正常工作。

        public class GoodChatServer {
         ………
public static void main(String[] args) throws Exception {
    System.out.println("The chat server is running.");
    ServerSocket listener = new ServerSocket(PORT);
    try {
        ….
        }
    } finally {
    …..
    }
}
private static class Handler extends Thread {


   ……….
        this.socket = socket;
    }

   public void run() {
        try {

            in = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            while (true) {
                out.println("SUBMITNAME");
                name = in.readLine();
                if (name == null) {
               ..
                }

                synchronized (names) {
                    if (!names.contains(name)) {
                        names.add(name);
                        break;
                    }

There are a plethora of ways to display chat information from a Java server in a browser using JavaScript. 有很多方法可以使用JavaScript在浏览器中显示来自Java服务器的聊天信息。 Since you are already using sockets for your Java clients, one option would be to use WebSockets . 由于您已经在Java客户端中使用套接字,因此一种选择是使用WebSockets Oracle provides an introduction to Java Websockets here , which should help you with the server side of things. Oracle在这里提供了Java Websocket的介绍 ,它将帮助您解决服务器方面的问题。 Mozilla also has a tutorial for writing browser-based websockets here . Mozilla 在这里也有一个编写基于浏览器的Websocket的教程

Another option you may consider is to relay your data across global real-time network, such as PubNub. 您可能考虑的另一种选择是在全球实时网络(例如PubNub)上中继数据。 PubNub provides a Java API and JavaScript API which will allow you to publish messages from your Java server to your JavaScript clients, using code such as: PubNub提供了Java APIJavaScript API ,可使用以下代码将消息从Java服务器发布到JavaScript客户端:

<script src="http://cdn.pubnub.com/pubnub.min.js"></script>
<script>(function(){

var pubnub = PUBNUB.init({
    publish_key   : 'demo',
    subscribe_key : 'demo'
})

pubnub.subscribe({
    channel : "my_chat_channel",
    message : function(m){ alert(m) }, //Display the chat message
})});</script>

On the Java server, you'd write your publish code: 在Java服务器上,您将编写发布代码:

Pubnub pubnub = new Pubnub("demo", "demo");

Callback callback = new Callback() {
    public void successCallback(String channel, Object response) {
        System.out.println(response.toString());
    }
    public void errorCallback(String channel, PubnubError error) {
        System.out.println(error.toString());
    }
};
pubnub.publish("my_chat_channel", "Here is my amazing chat message!" , callback);

PubNub is currently free for up to 1 million messages per month . 目前 PubNub是免费的,每月最多可发送一百万条消息 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM