簡體   English   中英

websockets api中的.sendText()和System.out.println有什么區別

[英]What is difference between .sendText() and System.out.println in websockets api

我是Java HTML5 websockets api的初學者。

我遇到了以下問題,請問有人可以告訴我

session.getBasicRemote().sendText("Hello");

System.out.println(hello);

代碼是:

@OnOpen
public void onOpen(Session session) {
    System.out.println("Connected to endpoint: " + session.getBasicRemote());
    try {
        session.getBasicRemote().sendText("Hello");
    } catch (IOException ex) {
    }
}

@OnMessage
public void onMessage(String message) {
    System.out.println(message);
}

謝謝你的幫助。

System.out.println("hello"); 僅將字符串寫入控制台,也就是說,例如,如果您在Windows環境中運行該字符串,則會導致控制台窗口彈出並帶有單詞“ Hello”。 通常用於調試目的。

sendText用於將字符串發送到客戶端,即與您的應用程序連接的某個人,例如遠程計算機或Web瀏覽器。

sendText是您應該使用的。 System.out.println對瀏覽器毫無價值。 您使用System.out.println打印的所有內容都不會離開服務器,並且不會被瀏覽器接收。

sendText()應該通過網絡套接字將提供的文本推送到在該套接字上偵聽的瀏覽器。 System.out.println()只是將數據打印到服務器的標准輸出(數據不會被推送到偵聽器)。

System.out.println(...)僅打印到本地控制台上下文。 如果沒有本地控制台,您將看不到任何輸出。

。session.getBasicRemote()sendText(...); 打印到遠程連接。

它們都執行文本打印,但是目標是兩個完全不同的目標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM