Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventBus onOpen Pub Subscribe with vertx3-eventbus-client #67

Closed
axlider opened this issue Mar 27, 2017 · 1 comment
Closed

EventBus onOpen Pub Subscribe with vertx3-eventbus-client #67

axlider opened this issue Mar 27, 2017 · 1 comment

Comments

@axlider
Copy link

axlider commented Mar 27, 2017

Hi,

i try to get data from server by listening to EventBus with SockJsHandler.
But my onopen function is never called.
In the network tab of chrome i can see these connection entries:

info?t=1490613267837 200 xhr abstract-xhr.js:128 367 B 19 ms
info?t=1490613267847 (canceled) xhr abstract-xhr.js:128 0 B 7.98 s
websocket 101 websocket websocket.js:32 0 B Pending

My application (webpack) runs on "https://localhost:3000" and listens to "http://localhost:8080/eventbus"

Server side code

`import java.util.concurrent.ExecutionException;

import io.vertx.core.Vertx;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.sockjs.BridgeOptions;
import io.vertx.ext.web.handler.sockjs.PermittedOptions;
import io.vertx.ext.web.handler.sockjs.SockJSHandler;

public class Server {

public void start(Vertx vertx, String host, int port) throws InterruptedException, ExecutionException {
	vertx.createHttpServer().requestHandler(httpRequest -> handleHttpRequest(vertx, httpRequest)).listen(8080);

	Router router = Router.router(vertx);
	SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
	BridgeOptions options = new BridgeOptions();
	options.addInboundPermitted(new PermittedOptions().setAddress("vehicleData"));
	options.addOutboundPermitted(new PermittedOptions().setAddress("vehicleData"));
	sockJSHandler.bridge(options);

	router.route("/eventbus/*").handler(sockJSHandler);
	// router.route().handler(io.vertx.ext.web.handler.CorsHandler.create("vertx\\.io").allowedMethod(HttpMethod.GET));
	router.route().handler(io.vertx.ext.web.handler.CorsHandler.create("*").allowedMethod(HttpMethod.GET));
	router.route().handler(io.vertx.ext.web.handler.CorsHandler.create("*").allowedMethod(HttpMethod.POST));
	
	EventBus eb = vertx.eventBus();

	vertx.setPeriodic(5000, v -> {
		eb.publish("vehicleData", getMessage());
		eb.send("vehicleData", getMessage());
		System.out.println("message sent ");
	});
}

private void handleHttpRequest(Vertx vertx, final HttpServerRequest httpRequest) {
	System.out.println("request handler");
}

public static void main(String[] args) throws InterruptedException, ExecutionException {
	Vertx vertx = Vertx.vertx();
	new Server().start(vertx, "localhost", 8080);
}

private String getMessage() {
		return "{\r\n" + 
			"   \"vehicles\": [\r\n" + 
			"      {\r\n" + 
			"         \"id\": \"1\",\r\n" + 
			"         \"vehicleid\": \"P110001\",\r\n" + 
			"         \"type\": \"N 5217 SHD\",\r\n" + 
			"         \"range\": \"BUSR\",\r\n" + 
			"         \"typekey\": \"N 5217 SHD\"\r\n" + 
			"      },\r\n" + 
			"      {\r\n" + 
			"         \"id\": \"2\",\r\n" + 
			"         \"vehicleid\": \"P110002\",\r\n" + 
			"         \"type\": \"N 5217 SHD\",\r\n" + 
			"         \"range\": \"BUSR\",\r\n" + 
			"         \"typekey\": \"N 5217 SHD\"\r\n" + 
			"      },\r\n" + 
			"      {\r\n" + 
			"         \"id\": \"3\",\r\n" + 
			"         \"vehicleid\": \"P110003\",\r\n" + 
			"         \"type\": \"N 5217 SHD\",\r\n" + 
			"         \"range\": \"BUSR\",\r\n" + 
			"         \"typekey\": \"N 5217 SHD\"\r\n" + 
			"      }\r\n" + 
			"   ]\r\n" + 
			"}";
}

}

Javascript code

`
const receiveEvents = function() {

var eb = new EventBus('http://localhost:8080/eventbus');

eb.onopen = () => {

    // set a handler to receive a message
    //eb.registerHandler('http://localhst:8080/vehicles/getVehicles', (error, message) => {
    eb.registerHandler('vehicleData', (error, message) => {
        console.log(`received a message: ${JSON.stringify(message)}`);
    });

    // send a message
    //eb.send('some-address', {name: 'tim', age: 587});
}
eb.onerror = function(e) {
    console.log('General error: ', e); // this does happen
}

}
receiveEvents();

`

thanks for any suggestions!

@axlider
Copy link
Author

axlider commented Mar 27, 2017

my fault. forgot to call : router.accept(httpRequest);
Everything works fine!

@axlider axlider closed this as completed Mar 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant