GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — websocket · Issue #473 · GoogleCloudPlatform/java-docs-samples

websocket #473

New issue
Copy link
New issue
Copy link
Closed
Closed
websocket#473
Copy link
Assignees
kurtisvg
Labels
🚨This issue needs some love.triage meI really want to be triaged.

Description

@kevendra
kevendra
opened on Dec 29, 2016
Issue body actions

websocket

I'm working on below sample code, how to use websockets on Google App Engine Flexible Environment with java.
as it mentions in the doc, in order to use websockets on App Engine, we need to connect directly to application instance using the instance's public external IP. This IP can be obtained from the metadata server.
but looks like I'm getting below 403 error when I'm trying to access metadata server

java.io.IOException: Server returned HTTP response code: 403 for URL: http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip

Sorry, It's working now, forgot to add Metadata-Flavor header in the request

still, it's worth having spring-websocket sample code, who want to use jGAE with spring websocket

Sample Code

Firewall rule

gcloud compute firewall-rules create default-allow-websockets \
  --allow tcp:65080 \
  --target-tags websocket \
  --description "Allow websocket traffic on port 65080"

app.yaml

runtime: java
env: flex

runtime_config:  # Optional
  jdk: openjdk8
  server: jetty9

handlers:
- url: /.*
  script: this field is required, but ignored
  secure: optional # always never optional # Require HTTPS

manual_scaling:
  instances: 1

network:
  forwarded_ports:
  - 65080
  instance_tag: websocket
  private static final int DEFAULT_PORT = 65080;
  private static final String NETWORK_INTERFACE_METADATA_URL = "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/" + "external-ip";

  private String getHostname() throws IOException {
	  String hostname = "localhost";
//      if (SystemProperty.environment.value().equals(SystemProperty.Environment.Value.Production)) {
        URL url = new URL(NETWORK_INTERFACE_METADATA_URL);
        HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
        httpUrlConnection.setRequestProperty("Metadata-Flavor", "Google");
//        httpUrlConnection.setRequestProperty("X-Google-Metadata-Request", "true");
        InputStream in = httpUrlConnection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String result, line = reader.readLine();
        result = line;
        while ((line = reader.readLine()) != null) {
          result += line;
        }
        hostname = result;
//      }
    return hostname;
  }

  /**
   * @return a Websocket URL of this server.
   * @throws IOException when failed to get the external IP address from the metadata server.
   */
  public String getWebSocketURL() throws IOException {
    return "ws://" + getHostname() + ":" + this.getPort() + "/";
  }

Ref

Architecture

Activity

  1. kevendra commented on Dec 31, 2016

    @kevendra
    kevendra
    on Dec 31, 2016
    Author
    More actions

    Open Issue:

    https://stackoverflow.com/questions/25708617/spring-4-websocket-configure-port

    Github Repo:

    https://github.com/kevendra/springmvc-websocket-sample

  2. kurtisvg
    self-assigned this
    on Jun 5, 2019
  3. kurtisvg commented on Jun 10, 2019

    @kurtisvg
    kurtisvg
    on Jun 10, 2019
    Contributor
    More actions

    Notice: this issue has been closed because it has been inactive for 890 days. You may reopen this issue if it has been closed in error.

  4. kurtisvg
    closed this as completedon Jun 10, 2019
  5. yoshi-automation
    added
    triage meI really want to be triaged.
    🚨This issue needs some love.
    on Apr 6, 2020
  6. Shabirmean
    added 2 commits that reference this issue on Nov 15, 2022

    chore(deps): update dependency com.google.cloud:google-cloud-document…

    5e5cfe7

    chore(deps): update dependency com.google.cloud:google-cloud-document…

    cedfc5f
  7. Sita04
    added a commit that references this issue on Feb 7, 2023

    chore: release 2.3.1 (#473)

    676009d
  8. Sita04
    added a commit that references this issue on Feb 17, 2023

    migrate code from googleapis/java-dlp (#7664)

    72b183d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @kurtisvg
    kurtisvg

Labels

🚨This issue needs some love.triage meI really want to be triaged.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.