All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ratpack.websocket.internal.DefaultWebSocketConnector Maven / Gradle / Ivy

There is a newer version: 2.0.0-rc-1
Show newest version
/*
 * Copyright 2013 the original author or 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.
 */

package ratpack.websocket.internal;

import ratpack.func.Action;
import ratpack.func.Function;
import ratpack.handling.Context;
import ratpack.server.ServerConfig;
import ratpack.websocket.*;

public class DefaultWebSocketConnector implements WebSocketConnector {

  private final Context context;
  private final Function open;

  private class Spec implements WebSocketSpec {
    private Action> messageHandler = Action.noop();
    private Action> closeHandler = Action.noop();

    private String path = "/";
    private int maxLength;

    private Spec(int maxLength) {
      this.maxLength = maxLength;
    }

    @Override
    public WebSocketSpec path(String path) {
      this.path = path;
      return this;
    }

    @Override
    public WebSocketSpec onClose(Action> action) {
      this.closeHandler = action;
      return this;
    }

    @Override
    public WebSocketSpec onMessage(Action> action) {
      messageHandler = action;
      return this;
    }

    @Override
    public WebSocketSpec maxLength(int maxLength) {
      this.maxLength = maxLength;
      return this;
    }
  }

  public DefaultWebSocketConnector(Context context, Function open) {
    this.context = context;
    this.open = open;
  }

  @Override
  public void connect(Action> specAction) throws Exception {
    Spec spec = new Spec(context.get(ServerConfig.class).getMaxContentLength());
    specAction.execute(spec);
    WebSocketEngine.connect(context, spec.path, spec.maxLength, new BuiltWebSocketHandler<>(open, spec.closeHandler, spec.messageHandler));

  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy