-
-
Save novabyte/20a5765d9867c23a27d5edac60223180 to your computer and use it in GitHub Desktop.
An example on how to authenticate and connect to Nakama server to play authoritative matches.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2018 The Nakama Authors | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
@Grapes([ | |
@Grab(group='com.squareup.moshi', module='moshi', version='1.5.0'), | |
@Grab(group='com.squareup.okhttp3', module='okhttp', version='3.10.0') | |
]) | |
import com.squareup.moshi.*; | |
import okhttp3.*; | |
import okio.*; | |
def moshi = new Moshi.Builder().build(); | |
def jsonAdapter = moshi.adapter(Map.class); | |
def client = new OkHttpClient(); | |
def mediaType = MediaType.parse("application/json; charset=utf-8"); | |
// Authenticate. | |
def sessions = []; | |
10.times { | |
def customId = UUID.randomUUID(); | |
def json = $/ | |
{ | |
"id": "${customId}" | |
} | |
/$; | |
def credential = Credentials.basic("defaultkey", ""); | |
def httpBody = RequestBody.create(mediaType, json); | |
def request = new Request.Builder() | |
.url("http://127.0.0.1:7349/v2/account/authenticate/custom?create=true") | |
.header("Authorization", credential) | |
.post(httpBody) | |
.build(); | |
def response = client.newCall(request).execute(); | |
def session = jsonAdapter.fromJson(response.body().string()); | |
sessions.add(session["token"]); | |
}; | |
println(sessions); | |
// Setup socket sessions. | |
def sockets = []; | |
sessions.each { session -> | |
def url = new HttpUrl.Builder() | |
.scheme("http") | |
.host("127.0.0.1") | |
.port(7349) | |
.addPathSegment("ws") | |
.addQueryParameter("token", session) | |
.build(); | |
def request = new Request.Builder().url(url).build(); | |
def socket = client.newWebSocket(request, new WebSocketListener() { | |
def NORMAL_CLOSURE_STATUS = 1000; | |
public void onOpen(WebSocket ws, Response response) { | |
sockets.add(ws); | |
} | |
public void onMessage(WebSocket ws, String text) { | |
def response = jsonAdapter.fromJson(text); | |
if (response.containsKey("rpc")) { | |
def newmatch = response["rpc"]["payload"]; | |
def matchid = jsonAdapter.fromJson(newmatch)["match_id"]; | |
def json = $/ | |
{ | |
"match_join": { | |
"match_id": "${matchid}" | |
} | |
} | |
/$; | |
sockets.each { joiner -> | |
joiner.send(json); | |
} | |
} else { | |
println(response); | |
} | |
} | |
public void onMessage(WebSocket ws, ByteString bytes) { | |
} | |
public void onClosing(WebSocket ws, int code, String reason) { | |
webSocket.close(NORMAL_CLOSURE_STATUS, "closed."); | |
} | |
public void onFailure(WebSocket ws, Throwable t, Response response) { | |
println(t.getMessage()); | |
} | |
}); | |
} | |
// Wait for all the sessions to connect a socket. | |
while(sockets.size() < sessions.size()) { | |
println("connected ${sockets.size()} sockets, need ${sessions.size()}") | |
sleep(100); | |
} | |
println("connected ${sockets.size()} sockets") | |
// Run authoritative matches. | |
10.times { | |
def json = $/ | |
{ | |
"rpc": { | |
"id": "p2prelayer.create", | |
"payload": null | |
} | |
} | |
/$; | |
sockets[0].send(json); | |
} | |
// Cleanup. | |
client.dispatcher().executorService().shutdown(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment