Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* The MIT License
* Copyright (c) 2015 Alexander Sova ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.codeminders.socketio.sample.chat;
import com.codeminders.socketio.server.*;
import com.codeminders.socketio.server.transport.jetty.JettySocketIOServlet;
import com.codeminders.socketio.common.DisconnectReason;
import com.codeminders.socketio.common.SocketIOException;
import com.google.common.io.ByteStreams;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import java.io.*;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ChatSocketServlet extends JettySocketIOServlet
{
private static final String ANNOUNCEMENT = "announcement"; // server to all connected clients
private static final String CHAT_MESSAGE = "chat message"; // broadcast to room
private static final String WELCOME = "welcome"; // single event sent by server to specific client
private static final String FORCE_DISCONNECT = "force disconnect"; // client requests server to disconnect
private static final String SERVER_BINARY = "server binary"; // client requests server to send a binary
private static final String CLIENT_BINARY = "client binary"; // client sends binary
private static final Logger LOGGER = Logger.getLogger(ChatSocketServlet.class.getName());
private static final long serialVersionUID = 1L;
@Override
@SuppressWarnings("unchecked")
public void init(ServletConfig config) throws ServletException
{
JdkOverLog4j.install();
super.init(config);
of("/chat").on(new ConnectionListener()
{
@Override
public void onConnect(final Socket socket)
{
try
{
socket.emit(WELCOME, "Welcome to Socket.IO Chat, " + socket.getRequest().getRemoteAddr() + "!");
socket.join("room");
}
catch (SocketIOException e)
{
e.printStackTrace();
socket.disconnect(true);
}
socket.on(new DisconnectListener() {
@Override
public void onDisconnect(Socket socket, DisconnectReason reason, String errorMessage)
{
of("/chat").emit(ANNOUNCEMENT, socket.getSession().getSessionId() + " disconnected");
}
});
socket.on(CHAT_MESSAGE, new EventListener()
{
@Override
public Object onEvent(String name, Object[] args, boolean ackRequested)
{
LOGGER.log(Level.FINE, "Received chat message: " + args[0]);
try
{
socket.broadcast("room", CHAT_MESSAGE, socket.getId(), args[0]);
}
catch (SocketIOException e)
{
e.printStackTrace();
}
return "OK"; //this object will be sent back to the client in ACK packet
}
});
socket.on(FORCE_DISCONNECT, new EventListener()
{
@Override
public Object onEvent(String name, Object[] args, boolean ackRequested)
{
socket.disconnect(false);
return null;
}
});
socket.on(CLIENT_BINARY, new EventListener()
{
@Override
public Object onEvent(String name, Object[] args, boolean ackRequested)
{
Map map = (Map