Skip to content

Instantly share code, notes, and snippets.

@gurpreet-
Last active January 28, 2020 18:34
Show Gist options
  • Save gurpreet-/24b850639dc32e655792 to your computer and use it in GitHub Desktop.
Save gurpreet-/24b850639dc32e655792 to your computer and use it in GitHub Desktop.
Sending "Hello" from Android or Java to Primus in binary or string form
package com.company;
import com.neovisionaries.ws.client.*;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class Main extends Thread {
private WebSocket webSocket;
public static void main(String[] args) {
new Main().start();
}
@Override
public void run() {
try {
connect();
} catch (IOException e) {
e.printStackTrace();
} catch (WebSocketException e) {
e.printStackTrace();
}
}
private void l(String s) {
System.out.println(s);
}
// Add listeners to the websocket.
WebSocketAdapter websocketAdapter = new WebSocketAdapter() {
@Override
public void onTextMessage(WebSocket websocket, String message) throws Exception {
// Received a text message.
l("String message from server: " + message);
}
@Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
super.onConnected(websocket, headers);
l("Websocket connected");
// Send hello to the server in binary
byte[] b = "Hello".getBytes();
websocket.sendBinary(b);
// Send hello to the server as a string
websocket.sendText("Hello");
}
@Override
public void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception {
super.onBinaryMessage(websocket, binary);
l("Binary Message from server: " + new String(binary));
}
@Override
public void onPingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
super.onPingFrame(websocket, frame);
l("Got a ping from server");
}
@Override
public void onPongFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
super.onPongFrame(websocket, frame);
l("Got pong from server");
}
@Override
public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
super.onError(websocket, cause);
cause.printStackTrace();
}
};
// Start the connection. Should be called in a new thread
public void connect() throws IOException, WebSocketException {
WebSocketFactory factory = new WebSocketFactory();
webSocket = factory.createSocket(generatePrimusUrl());
webSocket.addListener(websocketAdapter);
webSocket.setPingPayloadGenerator(new PayloadGenerator() {
@Override
public byte[] generate() {
return ("primus::ping::" + new Date().toString()).getBytes();
}
});
webSocket.setPingInterval(10 * 1000);
webSocket.connect();
}
private String generatePrimusUrl() {
return "ws://localhost:10001/primus";
}
}
{
"name": "server",
"version": "1.0.0",
"description": "test",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node test.js"
},
"author": "me",
"license": "ISC",
"dependencies": {
"binary-pack": "^1.0.2",
"http": "0.0.0",
"primus": "^4.0.5",
"primus-emit": "^1.0.0",
"primus-rooms": "^3.3.0",
"ws": "^1.0.1"
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>something</groupId>
<artifactId>samplewebsocketclient</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.neovisionaries</groupId>
<artifactId>nv-websocket-client</artifactId>
<version>1.22</version>
</dependency>
</dependencies>
</project>
var Primus = require('primus');
var server_port = 10001;
var parser = {
encoder: function encode(data, callback) {
var err = null;
callback(err, data);
},
decoder: function decode(data, callback) {
var err = null;
callback(err, data);
}
};
Primus.createServer(listener,
{
port: server_port,
transformer: 'websockets',
parser: parser
}
);
function listener(spark) {
spark.on('data', function (data) {
console.log(data);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment