Java Socket 和 JS WebSocket [英] Java Socket and JS WebSocket

查看:105
本文介绍了Java Socket 和 JS WebSocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图在我的 Java 应用程序和我的 Web 应用程序之间建立某种连接,我查找了 websockets,它们看起来非常简单且易于使用:).我为自己创建了一个 Java Server,它使用 ServerSocket 类.

So I am trying to make some sort of connection between my Java app and my Web app, I looked up websockets and they look really simple and easy to use :). And I created myself a Java Server, which uses the ServerSocket class.

现在的问题是我可以使用 websocket 从网络连接到服务器,但是我无法将数据发送到服务器......但是当我尝试从 Java 客户端发送数据时,它工作正常...可能是什么问题?

Now the problem is I am able to connect to the server from the web, with the websocket, but I am unable to send data to the server... but when I tried to send data from a Java Client it worked fine... what might be the problem?

我的 Java/Scala(我遵循了本教程:https://www.tutorialspoint.com/java/java_networking.htm) 服务器:

My Java/Scala (I followed this tutorial: https://www.tutorialspoint.com/java/java_networking.htm) server:

class Server(val port: Int) extends Thread {

  private val serverSocket = new ServerSocket(port)

  override def run(): Unit = {
    try {
      while(true) {
        println("Waiting for client on port: " + serverSocket.getLocalPort)
        val server = serverSocket.accept()

        println(server.getRemoteSocketAddress)
        val in = new DataInputStream(server.getInputStream())
        println(in.readUTF())
        val out = new DataOutputStream(server.getOutputStream())
        out.writeUTF("Hello world!")
        server.close()
      }
    } catch {
      case s: SocketTimeoutException => println("Connection timed out!");
      case e: Exception => e.printStackTrace()
    }
  }
}

我的 web js(我遵循了 https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications ):

My web js (I followed https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications ):

/**
 * Created by samuelkodytek on 20/12/2016.
 */
var conn = new WebSocket('ws://127.0.0.1:8080');

conn.onopen = function(e) {
    console.log("Connection established!");
    conn.send("Hello!");
};

conn.onmessage = function(e) {
    console.log(e.data);
};

推荐答案

Web 套接字服务器与简单的套接字服务器不同.提供网络套接字的服务器必须首先提供 HTTP 或 HTTPS 服务,因为网络套接字是在网络客户端发送带有升级选项和用于建立网络套接字的特殊字段的 HTTP 请求时建立的.即使在 Web 套接字建立之后,连接的行为仍然与常规套接字不同.Web Socket 协议使用帧来发送或接收数据.这与您所期望的完全不同.

A web socket server is not the same thing as a simple socket server. A server that offers web sockets must first offer HTTP or HTTPS services because the web socket is established when a web client sends an HTTP request with an Upgrade option and special fields for establishing the web socket. Even after the web socket is established, the connection still does not behave exactly like a regular socket. The Web Socket protocol uses frames to send or receive data. This is all considerably different from what you seem to expect.

您应该注意的另一件事是浏览器会强制执行规则,即 Web 套接字必须与尝试建立 Web 套接字的页面来自同一主机(相同的协议、地址和 TCP端口).

One other thing that you should be aware of is that the browser will enforce the rule that the web socket must come from the same host as the page that is attempting to establish the web socket (the same protocol, address, and TCP port).

这篇关于Java Socket 和 JS WebSocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆