-
Notifications
You must be signed in to change notification settings - Fork 10
/
ChatServlet.java
38 lines (29 loc) · 1.32 KB
/
ChatServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.twilio;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;
@SuppressWarnings("serial")
public class ChatServlet extends HttpServlet {
//Generate a client token for the GAE Channel API
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
ChannelService channelService = ChannelServiceFactory.getChannelService();
//The token must be based on some unique identifier for the client - I am using the current time
//for demo purposes...
String clientId = String.valueOf(System.currentTimeMillis());
String token = channelService.createChannel(clientId);
//Subscribe this client
MultichannelChatManager.addSub(clientId);
//Reply with the token
res.setContentType("text/plain");
res.getWriter().print(token);
}
//Publish a chat message to all subscribers, if this subscriber exists
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
MultichannelChatManager.sendMessage(req.getParameter("message"), "web");
res.setContentType("text/plain");
res.getWriter().print("yep.");
}
}