Skip to content

Instantly share code, notes, and snippets.

@mhaemmerle
Created June 27, 2011 20:09
Show Gist options
  • Save mhaemmerle/1049708 to your computer and use it in GitHub Desktop.
Save mhaemmerle/1049708 to your computer and use it in GitHub Desktop.
netty upgrade request
import java.net.InetSocketAddress
import java.util.concurrent.Executors
import org.jboss.netty.bootstrap.ClientBootstrap
import org.jboss.netty.buffer.ChannelBuffers
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory
import org.jboss.netty.channel._
import org.jboss.netty.util.CharsetUtil
import org.jboss.netty.handler.codec.http.HttpClientCodec
object Client {
def main(args: Array[String]) {
val host = if (args.length > 0) args(0) else "localhost"
val port = if (args.length > 1) args(1).toInt else 8080
val factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool, Executors.newCachedThreadPool)
val bootstrap = new ClientBootstrap(factory)
bootstrap.setPipelineFactory(new ClientPipelineFactory())
val future = bootstrap.connect(new InetSocketAddress(host, port))
future.getChannel.getCloseFuture.awaitUninterruptibly()
bootstrap.releaseExternalResources()
}
}
class ClientPipelineFactory extends ChannelPipelineFactory {
@throws(classOf[Exception])
def getPipeline: ChannelPipeline = {
val pipeline: ChannelPipeline = Channels.pipeline
pipeline.addLast("codec", new HttpClientCodec())
pipeline.addLast("handler", new ClientHandler())
pipeline
}
}
class ClientHandler extends SimpleChannelUpstreamHandler {
val NEWLINE = "\r\n"
val notWorkingRequest = "GET /ws/ HTTP/1.1" + NEWLINE +
"Upgrade: WebSocket" + NEWLINE +
"Connection: Upgrade" + NEWLINE +
"Host: localhost" + NEWLINE +
"Origin: http://localhost" + NEWLINE +
"Sec-WebSocket-Key1: " + NEWLINE +
"Sec-WebSocket-Key2: " + NEWLINE + NEWLINE
val workingRequest = notWorkingRequest + "12345678"
override def channelConnected(ctx: ChannelHandlerContext, e: ChannelStateEvent) {
e.getChannel.write(ChannelBuffers.copiedBuffer(notWorkingRequest, CharsetUtil.US_ASCII))
}
override def exceptionCaught(ctx: ChannelHandlerContext, e: ExceptionEvent) {
e.getCause.printStackTrace()
e.getChannel.close()
}
}
import java.net.InetSocketAddress
import java.util.concurrent.Executors
import org.jboss.netty.bootstrap.ServerBootstrap
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory
import org.jboss.netty.handler.codec.http.{HttpRequestDecoder, HttpResponseEncoder}
import org.jboss.netty.channel._
object Server {
def main(args: Array[String]) {
val port = if (args.length > 0) args(0).toInt else 8080
val factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool())
val bootstrap = new ServerBootstrap(factory)
bootstrap.setPipelineFactory(new ServerPipelineFactory())
bootstrap.bind(new InetSocketAddress(port))
}
}
class ServerPipelineFactory extends ChannelPipelineFactory {
@throws(classOf[Exception])
def getPipeline: ChannelPipeline = {
val pipeline: ChannelPipeline = Channels.pipeline
pipeline.addLast("decoder", new HttpRequestDecoder())
pipeline.addLast("encoder", new HttpResponseEncoder())
pipeline.addLast("handler", new ServerHandler())
pipeline
}
}
class ServerHandler extends SimpleChannelUpstreamHandler {
@throws(classOf[Exception])
override def channelConnected(ctx: ChannelHandlerContext, e: ChannelStateEvent) {
println(this + " channelConnected")
super.channelConnected(ctx, e)
}
override def messageReceived(ctx: ChannelHandlerContext, e: MessageEvent) {
println(this + " messageReceived")
}
override def exceptionCaught(ctx: ChannelHandlerContext, e: ExceptionEvent) {
e.getCause.printStackTrace()
e.getChannel.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment