Showing posts with label Netty. Show all posts
Showing posts with label Netty. Show all posts

Wednesday, August 6, 2014

Netty detect when client network changed and closed -> PING, PONG

This guide is for the users who are already used to using netty


Actually I tried to make chatting program with Netty
I was very happy to know netty and it seems to be very good for speed and connections

but I faced with some problem
When Android Client side Stop application exit it Serverside relize that client is disconnected.
but when client change 4g to wifi client connection lost, however Serverside dosen't know about client lost.
noting happend.

so I google and found some good handler .
this is ping periodically .
and server side finally exactly know whether specific client is where alive or not.

=== Explain ==

idleStateHandler

readerIdleTimean IdleStateEvent whose state is IdleState.READER_IDLE will be triggered when no read was performed for the specified period of time. Specify 0 to disable.
writerIdleTimean IdleStateEvent whose state is IdleState.WRITER_IDLE will be triggered when no write was performed for the specified period of time. Specify 0 to disable.
allIdleTimean IdleStateEvent whose state is IdleState.ALL_IDLE will be triggered when neither read nor write was performed for the specified period of time. Specify 0 to disable.


pipeline.addLast("idleStateHandler", new IdleStateHandler(60, 30, 0));        pipeline.addLast("myHandler", new MyHandler());



class MyHandler extends ChannelDuplexHandler {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            if (e.state() == IdleState.READER_IDLE) {
                System.out.println("reader ");
                System.out.println("reader ");
                System.out.println("reader ");
                System.out.println("reader ");
            ctx.close();
            } else if (e.state() == IdleState.WRITER_IDLE) {
                 try{
             ctx.channel().writeAndFlush("ping:"+ctx.channel().hashCode() );
                 }catch(Exception es){
                 es.printStackTrace();
                 }
             System.out.println("writer ");
                 System.out.println("writer ");
            }
        }
    }
}

Thursday, June 12, 2014

Netty (2)

Yesterday I started  netty becasue of trying to make game server

And I read the article why multi networking program recommend netty

yeseter day i Make chatting source and

what i made are below list

This is My Chat JavaAPI

id:id  
= Make User Id

s:room_seq:message 
=  Write message in special room

m:name 
= make room and write name
mo: 
= out room

in:room_seq 
= enter the chat room

bye: 
= exit chatting

sh: 
= show user
sr : 
= user room


and I will make more apis

which are queries like general database

My Finall purpose is make rooms when admin want to make and

all around people can access rooms if they download Android application.

after completion of chatting server,
I will open API and source for you guys and relasese for free.

Wednesday, June 11, 2014

Netty ~ (1)

Today I start to study Netty

this is well-known for networking program like chatting or game server and so on

this is NIO means non blocking

and I will write what i learn from netty~

expect ~~ haha