Skip to content

Using the WebSocket through a http proxy

Chen edited this page Aug 6, 2020 · 2 revisions

Introduction

You can use a websocket through a http proxy.

Example

The following example was provided by shuckc

publicstaticvoidmain( String[] args ) throwsException{URIserverUri = newURI( "wss://ourapp.herokuapp.com:443/ws1" ); WebSocketChatClientchatclient = newWebSocketChatClient( serverUri ); Proxyproxy = newProxy( Proxy.Type.HTTP, newInetSocketAddress( "proxy.corporate.com", 2128) ); chatclient.setProxy( proxy ); SSLContextsslContext = null; sslContext = SSLContext.getInstance( "TLS" ); sslContext.init( null, null, null ); SSLSocketFactoryfactory = sslContext.getSocketFactory(); chatclient.setSocketFactory( factory ); chatclient.connectBlocking(); BufferedReaderreader = newBufferedReader( newInputStreamReader( System.in ) ); while ( true ){Stringline = reader.readLine(); if( line.equals( "close" ) ){chatclient.close()} else{chatclient.send("{\"msg\":\"" + line + "\"}")} } }

Clone this wiki locally