WebSocket Get page pass-through @pathparam when establishing a connection

Source: Internet
Author: User

Recently using Java under the WebSocket, there is a requirement, when the page and Java background to establish a connection to get the parameters provided on the page, that is, in the method of @onopen annotations to get the parameters of the page, there is a very simple way to get. Even with @pathparam annotations.


The first step:

The annotations in the Java class that are mapped by the WebSocket are as follows:

@ServerEndpoint ("/websocket/{relationid}/{usercode}")

whichWebSocketis the map address

/{relationid}/{usercode} is two parameters that can be passed to the page, respectively


Step Two:

In the method in the Java class:

This allows you to get the values of the two parameters in the first step (Relationid and Usercode)


Step Three:

In the JavaScript script

var ipport = Window.location.host;
Chat.initialize = function () {if (Window.location.protocol = = ' http: ') {chat.connect (' ws://' + ipport+ '/aninvestigation /websocket/1/2 ');} else {chat.connect (' wss://' + ipport+ '/aninvestigation/websocket/1/2 ');}};
which/ANINVESTIGATION/WEBSOCKET/1/2"1" and "2" are custom parameters.


The 3 steps above will allow you to pass the value of the page into Java at once.




Here's the code and run:





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

WebSocket Get page pass-through @pathparam when establishing a connection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.