|
| 1 | +<!DOCTYPE html> |
| 2 | +<!-- |
| 3 | + | Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/faq.md at 2022-07-02 |
| 4 | + | Rendered using Apache Maven Fluido Skin 1.9 |
| 5 | +--> |
| 6 | +<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> |
| 7 | + <head> |
| 8 | + <meta charset="UTF-8" /> |
| 9 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 10 | + <meta name="generator" content="Apache Maven Doxia Site Renderer 1.9.2" /> |
| 11 | + <title>socket.io-client – Frequently asked questions</title> |
| 12 | + <link rel="stylesheet" href="./css/apache-maven-fluido-1.9.min.css" /> |
| 13 | + <link rel="stylesheet" href="./css/site.css" /> |
| 14 | + <link rel="stylesheet" href="./css/print.css" media="print" /> |
| 15 | + <script src="./js/apache-maven-fluido-1.9.min.js"></script> |
| 16 | + </head> |
| 17 | + <body class="topBarDisabled"> |
| 18 | + <a href="https://github.com/socketio/socket.io-client-java"> |
| 19 | + <img style="position: absolute; top: 0; right: 0; border: 0; z-index: 10000;" |
| 20 | + src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" |
| 21 | + alt="Fork me on GitHub"> |
| 22 | + </a> |
| 23 | + <div class="container-fluid"> |
| 24 | + <header> |
| 25 | + <div id="banner"> |
| 26 | + <div class="pull-left"><div id="bannerLeft"><h2>Socket.IO Java client</h2> |
| 27 | +</div> |
| 28 | +</div> |
| 29 | + <div class="pull-right"></div> |
| 30 | + <div class="clear"><hr/></div> |
| 31 | + </div> |
| 32 | + |
| 33 | + <div id="breadcrumbs"> |
| 34 | + <ul class="breadcrumb"> |
| 35 | + <li id="publishDate">Last Published: 2022-07-02<span class="divider">|</span> |
| 36 | +</li> |
| 37 | + <li id="projectVersion">Version: 2.0.2-SNAPSHOT</li> |
| 38 | + </ul> |
| 39 | + </div> |
| 40 | + </header> |
| 41 | + <div class="row-fluid"> |
| 42 | + <header id="leftColumn" class="span2"> |
| 43 | + <nav class="well sidebar-nav"> |
| 44 | + <ul class="nav nav-list"> |
| 45 | + <li class="nav-header">Overview</li> |
| 46 | + <li><a href="installation.html" title="Installation"><span class="none"></span>Installation</a></li> |
| 47 | + <li><a href="initialization.html" title="Initialization"><span class="none"></span>Initialization</a></li> |
| 48 | + <li><a href="emitting_events.html" title="Emitting events"><span class="none"></span>Emitting events</a></li> |
| 49 | + <li><a href="listening_to_events.html" title="Listening to events"><span class="none"></span>Listening to events</a></li> |
| 50 | + <li><a href="socket_instance.html" title="The Socket instance"><span class="none"></span>The Socket instance</a></li> |
| 51 | + <li><a href="migrating_from_1_x.html" title="Migrating from 1.x"><span class="none"></span>Migrating from 1.x</a></li> |
| 52 | + <li class="active"><a href="#"><span class="none"></span>FAQ</a></li> |
| 53 | + <li class="nav-header">Miscellaneous</li> |
| 54 | + <li><a href="changelog.html" title="Changelog"><span class="none"></span>Changelog</a></li> |
| 55 | + <li><a href="apidocs/index.html" title="Javadoc"><span class="none"></span>Javadoc</a></li> |
| 56 | + <li class="nav-header">Project Documentation</li> |
| 57 | + <li><a href="project-info.html" title="Project Information"><span class="icon-chevron-right"></span>Project Information</a></li> |
| 58 | + </ul> |
| 59 | + </nav> |
| 60 | + <div class="well sidebar-nav"> |
| 61 | + <hr /> |
| 62 | + <div id="poweredBy"> |
| 63 | + <div class="clear"></div> |
| 64 | + <div class="clear"></div> |
| 65 | + <div class="clear"></div> |
| 66 | +<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy"><img class="builtBy" alt="Built by Maven" src="./images/logos/maven-feather.png" /></a> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + </header> |
| 70 | + <main id="bodyColumn" class="span10" > |
| 71 | +<h1>Frequently asked questions</h1> |
| 72 | +<ul> |
| 73 | +<li><a href="#How_to_deal_with_cookies">How to deal with cookies</a></li> |
| 74 | +<li><a href="#How_to_use_with_AWS_Load_Balancing">How to use with AWS Load Balancing</a></li></ul> |
| 75 | +<section> |
| 76 | +<h2><a name="How_to_deal_with_cookies"></a>How to deal with cookies</h2> |
| 77 | +<p>In order to store the cookies sent by the server and include them in all subsequent requests, you need to create an OkHttpClient with a custom <a class="externalLink" href="https://square.github.io/okhttp/4.x/okhttp/okhttp3/-cookie-jar/">cookie jar</a>.</p> |
| 78 | +<p>You can either implement your own cookie jar:</p> |
| 79 | + |
| 80 | +<div class="source"> |
| 81 | +<div class="source"><pre class="prettyprint">public class MyApp { |
| 82 | + |
| 83 | + public static void main(String[] argz) throws Exception { |
| 84 | + IO.Options options = new IO.Options(); |
| 85 | + |
| 86 | + OkHttpClient okHttpClient = new OkHttpClient.Builder() |
| 87 | + .cookieJar(new MyCookieJar()) |
| 88 | + .build(); |
| 89 | + |
| 90 | + options.callFactory = okHttpClient; |
| 91 | + options.webSocketFactory = okHttpClient; |
| 92 | + |
| 93 | + Socket socket = IO.socket(URI.create("https://example.com"), options); |
| 94 | + |
| 95 | + socket.connect(); |
| 96 | + } |
| 97 | + |
| 98 | + private static class MyCookieJar implements CookieJar { |
| 99 | + private Set<WrappedCookie> cache = new HashSet<>(); |
| 100 | + |
| 101 | + @Override |
| 102 | + public void saveFromResponse(HttpUrl url, List<Cookie> cookies) { |
| 103 | + for (Cookie cookie : cookies) { |
| 104 | + this.cache.add(new WrappedCookie(cookie)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public List<Cookie> loadForRequest(HttpUrl url) { |
| 110 | + List<Cookie> cookies = new ArrayList<>(); |
| 111 | + Iterator<WrappedCookie> iterator = this.cache.iterator(); |
| 112 | + while (iterator.hasNext()) { |
| 113 | + Cookie cookie = iterator.next().cookie; |
| 114 | + if (isCookieExpired(cookie)) { |
| 115 | + iterator.remove(); |
| 116 | + } else if (cookie.matches(url)) { |
| 117 | + cookies.add(cookie); |
| 118 | + } |
| 119 | + } |
| 120 | + return cookies; |
| 121 | + } |
| 122 | + |
| 123 | + private static boolean isCookieExpired(Cookie cookie) { |
| 124 | + return cookie.expiresAt() < System.currentTimeMillis(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private static class WrappedCookie { |
| 129 | + private final Cookie cookie; |
| 130 | + |
| 131 | + public WrappedCookie(Cookie cookie) { |
| 132 | + this.cookie = cookie; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public boolean equals(Object o) { |
| 137 | + if (!(o instanceof WrappedCookie)) return false; |
| 138 | + WrappedCookie that = (WrappedCookie) o; |
| 139 | + return that.cookie.name().equals(this.cookie.name()) |
| 140 | + && that.cookie.domain().equals(this.cookie.domain()) |
| 141 | + && that.cookie.path().equals(this.cookie.path()) |
| 142 | + && that.cookie.secure() == this.cookie.secure() |
| 143 | + && that.cookie.hostOnly() == this.cookie.hostOnly(); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public int hashCode() { |
| 148 | + int hash = 17; |
| 149 | + hash = 31 * hash + cookie.name().hashCode(); |
| 150 | + hash = 31 * hash + cookie.domain().hashCode(); |
| 151 | + hash = 31 * hash + cookie.path().hashCode(); |
| 152 | + hash = 31 * hash + (cookie.secure() ? 0 : 1); |
| 153 | + hash = 31 * hash + (cookie.hostOnly() ? 0 : 1); |
| 154 | + return hash; |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | +</pre></div></div> |
| 159 | + |
| 160 | +<p>Or use a package like <a class="externalLink" href="https://github.com/franmontiel/PersistentCookieJar">PersistentCookieJar</a>:</p> |
| 161 | + |
| 162 | +<div class="source"> |
| 163 | +<div class="source"><pre class="prettyprint">public class MyApp { |
| 164 | + |
| 165 | + public static void main(String[] argz) throws Exception { |
| 166 | + IO.Options options = new IO.Options(); |
| 167 | + |
| 168 | + ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context)); |
| 169 | + |
| 170 | + OkHttpClient okHttpClient = new OkHttpClient.Builder() |
| 171 | + .cookieJar(cookieJar) |
| 172 | + .build(); |
| 173 | + |
| 174 | + options.callFactory = okHttpClient; |
| 175 | + options.webSocketFactory = okHttpClient; |
| 176 | + |
| 177 | + Socket socket = IO.socket(URI.create("https://example.com"), options); |
| 178 | + |
| 179 | + socket.connect(); |
| 180 | + } |
| 181 | +} |
| 182 | +</pre></div></div> |
| 183 | +</section><section> |
| 184 | +<h2><a name="How_to_use_with_AWS_Load_Balancing"></a>How to use with AWS Load Balancing</h2> |
| 185 | +<p>When scaling to multiple Socket.IO servers, you must ensure that all the HTTP requests of a given session reach the same server (explanation <a class="externalLink" href="https://socket.io/docs/v4/using-multiple-nodes/#why-is-sticky-session-required">here</a>).</p> |
| 186 | +<p>Sticky sessions can be enabled on AWS Application Load Balancers, which works by sending a cookie (<code>AWSALB</code>) to the client.</p> |
| 187 | +<p>Please see <a href="#how-to-deal-with-cookies">above</a> for how to deal with cookies.</p> |
| 188 | +<p>Reference: <a class="externalLink" href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/sticky-sessions.html">https://docs.aws.amazon.com/elasticloadbalancing/latest/application/sticky-sessions.html</a></p></section> |
| 189 | + </main> |
| 190 | + </div> |
| 191 | + </div> |
| 192 | + <hr/> |
| 193 | + <footer> |
| 194 | + <div class="container-fluid"> |
| 195 | + <div class="row-fluid"> |
| 196 | + <p>© 2022 |
| 197 | +</p> |
| 198 | + </div> |
| 199 | + </div> |
| 200 | + </footer> |
| 201 | + </body> |
| 202 | +</html> |
0 commit comments