2009年12月25日金曜日

[HTML5] 簡単なWebSocketサーバを作ってみた。

HTML5のWebSocketサーバを作ってみた。

作ったのは これ



作ったサーバでシンプルなEchoサーバとクライアントも作ってみたのがこちら。。
まだWebSocketを対応してるのがChromeしかないので、当然Chromeしか動作しない。

EchoServer.java

package websocket.echoserver;

import java.io.IOException;

import com.babukuma.commons.websocket.WebSocketServer;

public class EchoServer {
public static void main(String[] args) throws IOException {
new WebSocketServer(8888, "/EchoServer", new EchoHandlerFactory()).start();
}
}

EchoHandlerFactory.java

package websocket.echoserver;

import com.babukuma.commons.websocket.WebSocketHandler;
import com.babukuma.commons.websocket.WebSocketHandlerFactory;

public class EchoHandlerFactory implements WebSocketHandlerFactory {

@Override
public WebSocketHandler createHandler(String urlPath) {
return new EchoHandler();
}

@Override
public void removeHandler(String urlPath, WebSocketHandler handler) {
// TODO Auto-generated method stub
}
}

EchoHandler.java

package websocket.echoserver;

import com.babukuma.commons.websocket.SimpleWebSocketHandler;

public class EchoHandler extends SimpleWebSocketHandler {

@Override
public void onMessage(String message) {
send
(message);
}

@Override
public void onClose() {
System.out.println("close");
}

@Override
public void onError(Throwable e) {
e
.printStackTrace();
}

@Override
public void onOpen() {
System.out.println("open");
}
}


次はTomcatとかWebServerでも動けるようにWebSocketServletを作成!!


2009年12月11日金曜日

【メモ】sendmailが送信できない

今回新しくセットアップしたサーバからsendmailからメールが送信できなく

stat=Service unavailable

エラーになってしまった。

/var/log/maillog」を調べてみたら友人のhiro_nemu様が

tcpwrappers (xxx.xxx.xx.jp, 127.0.0.1) rejection

「このへんがあやしいよ」って言って
/etc/hosts.allow」に

sendmail : all

を追加したら、メールが送信できるようになった。
これで解決。
allでいいのかどうかはわからないけど。。。

2009年12月1日火曜日

[メモ] TomcatインストルからApacheの連携まで

CentoOSにTomcatをインストしてApacheと連携する。


1. jdkのインストル


[root@srv ~]# wget -O jdk-6u17-linux-i586.bin http://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/VerifyItem-Start/jdk-6u17-linux-i586.bin?BundledLineItemUUID=Bp9IBe.pnKQAAAEl_DsdbUiv&OrderID=sxRIBe.pyHUAAAEl7TsdbUiv&ProductID=lBFIBe.oSOMAAAEkGehn5G0y&FileName=/jdk-6u17-linux-i586.bin
......

100%[======================================================================================================================================>] 81,792,634 538.62K/s ETA 00:00

14:40:36 (761.59 KB/s) - `jdk-6u17-linux-i586.bin' を保存しました [81792634/81792634]
....

[root@srv ~]# chmod 755 jdk-6u17-linux-i586.bin
[root@srv ~]# ./jdk-6u17-linux-i586.bin
....
Press Enter to continue.....


Done.

[root@srv ~]# mv jdk1.6.0_17/ /usr/lib/
[root@srv etc]# vi /etc/bashrc
export JAVA_HOME=/usr/lib/jdk1.6.0_17 <- 最後の行に追加




2. Tomcatのインストル


[root@srv ~]# wget http://www.meisei-u.ac.jp/mirror/apache/dist/tomcat/tomcat-6/v6.0.20/bin/apache-tomcat-6.0.20.zip
......

100%[======================================================================================================================================>] 6,379,115 400.23K/s ETA 00:00

14:56:15 (332.39 KB/s) - `apache-tomcat-6.0.20.zip' を保存しました [6379115/6379115]

[root@srv ~]# unzip -d /usr/local/ apache-tomcat-6.0.20.zip
[root@srv bin]# chmod +x /usr/local/apache-tomcat-6.0.20/bin/*.sh
[root@srv local]# ln -s /usr/local/apache-tomcat-6.0.20/ /usr/local/tomcat

[root@srv ~]# vi /etc/init.d/tomcat
#!/bin/sh
#
# Startup script for Apache Tomcat 6
#
# chkconfig: 2345 85 15
# description: Apache Tomcat Java Servlets and JSP server
# processname: tomcat
# pidfile: /var/run/tomcat.pid
# config: /usr/local/tomcat/conf/server.xml

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

CATALINA_HOME=/usr/local/tomcat
TOMCAT_SCRIPT=$CATALINA_HOME/bin/catalina.sh
TOMCAT_USER=root

[ -f $TOMCAT_SCRIPT ] || exit 0

# See how we were called.
case "$1" in
start)
# Start daemons.
if [ -f /var/lock/subsys/tomcat ] ; then
echo "Tomcat already started!"
exit 1
fi
echo -n "Starting Tomcat: "
su -s /bin/bash - $TOMCAT_USER -c "$TOMCAT_SCRIPT start"
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/tomcat
;;
stop)
# Stop daemons.
if [ ! -f /var/lock/subsys/tomcat ] ; then
echo "Tomcat already stopped!"
exit 1
fi
echo -n "Shutting down Tomcat: "
$TOMCAT_SCRIPT stop
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/tomcat
;;
status)
status tomcat
exit $?
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $TOMCAT_PROG {start|stop|status|restart}"
exit 1
esac

exit 0


[root@srv ~]# chmod +x /etc/init.d/tomcat



3. Apache Tomcat Connector の設定

[root@srv modules]# cd /etc/httpd/modules/
[root@srv modules]# wget http://ftp.kddilabs.jp/infosystems/apache/tomcat/tomcat-connectors/jk/binaries/linux/jk-1.2.28/i586/mod_jk-1.2.28-httpd-2.0.X.so
.....

100%[======================================================================================================================================>] 690,348 735.73K/s

15:06:02 (733.98 KB/s) - `mod_jk-1.2.28-httpd-2.0.X.so' を保存しました [690348/690348]

[root@srv modules]# chmod +x /etc/httpd/modules/mod_jk-1.2.28-httpd-2.0.X.so


- 「workers.properties」作成
[root@srv conf]# vi /etc/httpd/conf/workers.properties
# Define 1 real worker using ajp13
worker.list=babukumaWorker
# Set properties for worker1 (ajp13)
worker.
babukumaWorker.type=ajp13
worker.
babukumaWorker.host=localhost
worker.
babukumaWorker.port=8009

- 「httpd.conf」編集
[root@srv conf]# vi httpd.conf

LoadModule jk_module modules/mod_jk-1.2.28-httpd-2.0.X.so

# AJP
JkWorkersFile /etc/httpd/conf/workers.properties
# Where to put jk shared memory
# Update this path to match your local state directory or logs directory
JkShmFile /etc/httpd/logs/mod_jk.shm
# Where to put jk logs
# Update this path to match your logs directory location (put mod_jk.log next to access_log)
JkLogFile /etc/httpd/logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the timestamp log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# Send everything for context /examples to worker named worker1 (ajp13)
JkMount /examples* babukumaWorker