使用java和webgl进行安全套接字连接(wss)

时间:2018-03-08 07:35:04

标签: java c#

我使用WebGL成功尝试过ws。

这是我用c#编写的前端代码。

using System;
using System.Text;
using UnityEngine;

public class EchoExample : MonoBehaviour {

    UnityWebSocket WebSocket;

    // Use this for initialization
    void Start () {

        // This example service echoes back any messages sent to it
        WebSocket = new WebSocket("ws://localhost:8080/WebSocketServerExample/websocketendpoint");
        WebSocket.OnClose += WebSocket_OnClose;
        WebSocket.OnOpen += Websocket_OnOpen;
        WebSocket.OnMessage += WebSocket_OnMessage;
        WebSocket.OnError += WebSocket_OnError;
    }

    private void WebSocket_OnClose(UnityWebSocket sender, int code, string reason) {

        Debug.Log("Connection closed: " + reason);
    }

    private void Websocket_OnOpen(UnityWebSocket accepted) {

        Debug.Log("Connection established.");

        string message = "Hello, friend!";

        Debug.Log("Sending message: " + message);

        byte[] data = Encoding.UTF8.GetBytes(message);

        WebSocket.SendAsync(data);
    }

    private void WebSocket_OnMessage(UnityWebSocket sender, byte[] data) {

        string message = Encoding.UTF8.GetString(data);

        Debug.Log("Message received: " + message);
    }

    private void WebSocket_OnError(UnityWebSocket sender, string message) {
        Debug.Log("Error: " + message);
    }
}

这是我的java代码。

@ServerEndpoint("/websocketendpoint")
public class WsServer {

    @OnOpen
    public void onOpen(Session session){
        System.out.println("Open Connection ...");
         try {
             session.getBasicRemote().sendText("$ Kimaya Server 2003 $");
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    @OnClose
    public void onClose(Session session){
        System.out.println("Session " + session.getId() + " has ended");
    }

/*  @OnMessage
        public String onMessage(String s, Session session){
            System.out.println("Message from the client: " +s);
            return s;
         }
*/
    @OnMessage
    public void processUpload(byte[] bytes, boolean last, Session session) throws IOException {

        String s = new String(bytes);

        //  public String onMessage(String message, Session session){

//      byte in[] = message.getBytes();

            System.out.println("Message from the client: " +s);
            String welcomeString = "this is response from server";
            session.getBasicRemote().sendText(welcomeString);
            //      onMessage("from server", session);


    }

    @OnError
    public void onError(Throwable e){
        e.printStackTrace();
    }

}

我已经使用ws成功完成了通信,但现在下一步是我想用wss进行,即通过安全连接,经过大量搜索后仍然无法获得任何与此相关的示例或文档,是否有人尝试过之前??

0 个答案:

没有答案