Monthly Archives: June 2012

Adding project to Github

After two months, I decided to upload WebsocketDemo project to Github. Since then, I replaced my laptop and all the settings are gone. Also, I haven’t used Github for two months. Instead of using GUI, I used Git bash to upload my project.

Well, I made the same mistake again…. naming remote and origin differently, so I ended up deleting in both places. To do it properly, I should follow below steps:

1. Create Repository in Github. Skip readme.

2. Open Gitbash and navigate to c:/Users/msjo/GitRepo

3. Make repository directory same as the one in the Github.

4. Update README

5. Add origin to git remote

6. Push origin to remote master

msjo@MSJO-PC ~
$ cd gitrepo

msjo@MSJO-PC ~/gitrepo
$ ls

msjo@MSJO-PC ~/gitrepo
$ mkdir WebsocketDemo

msjo@MSJO-PC ~/gitrepo
$ cd WebsocketDemo

msjo@MSJO-PC ~/gitrepo/WebsocketDemo
$ git init
Initialized empty Git repository in c:/Users/msjo/gitrepo/WebsocketDemo/.git/

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$ touch README

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$ git add README

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$ git commit -m 'first commit'
[master (root-commit) 824a40b] first commit
 0 files changed
 create mode 100644 README

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$ git remote add origin https://github.com/mjtoolbox/WebsocketDemo.git

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$ git push -u origin master
usage: git credential-cache [options]

    --exit                tell a running daemon to exit
    --reject              reject a cached credential
    --timeout          number of seconds to cache credentials
    --socket        path of cache-daemon socket
    --chain       use  to get non-cached credentials
    --username      an existing username
    --description   human-readable description of the credential
    --unique       a unique context for the credential

Username for 'https://github.com':
Password for 'https://mjtoolbox@github.com':
usage: git credential-cache [options]

    --exit                tell a running daemon to exit
    --reject              reject a cached credential
    --timeout          number of seconds to cache credentials
    --socket        path of cache-daemon socket
    --chain       use  to get non-cached credentials
    --username      an existing username
    --description   human-readable description of the credential
    --unique       a unique context for the credential

fatal: Authentication failed

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$ git push -u origin master
usage: git credential-cache [options]

    --exit                tell a running daemon to exit
    --reject              reject a cached credential
    --timeout          number of seconds to cache credentials
    --socket        path of cache-daemon socket
    --chain       use  to get non-cached credentials
    --username      an existing username
    --description   human-readable description of the credential
    --unique       a unique context for the credential

Username for 'https://github.com':
Password for 'https://mjtoolbox@github.com':
usage: git credential-cache [options]

    --exit                tell a running daemon to exit
    --reject              reject a cached credential
    --timeout          number of seconds to cache credentials
    --socket        path of cache-daemon socket
    --chain       use  to get non-cached credentials
    --username      an existing username
    --description   human-readable description of the credential
    --unique       a unique context for the credential

To https://github.com/mjtoolbox/WebsocketDemo.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$ ls
README  WebContent  build  src

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$ git add .
warning: LF will be replaced by CRLF in WebContent/WEB-INF/faces-config.xml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in WebContent/WEB-INF/sun-web.xml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in WebContent/css/oneColFixCtr.css.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in WebContent/js/jquery-1.7.2.min.js.
The file will have its original line endings in your working directory.

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$ git status -s
A  .classpath
A  .metadata/WebContent/WEB-INF/faces-config.pageflow
A  .project
A  .settings/.jsdtscope
A  .settings/oracle.eclipse.tools.webtier.ui.prefs
A  .settings/org.eclipse.jdt.core.prefs
A  .settings/org.eclipse.wst.common.component
A  .settings/org.eclipse.wst.common.project.facet.core.prefs.xml
A  .settings/org.eclipse.wst.common.project.facet.core.xml
A  .settings/org.eclipse.wst.jsdt.ui.superType.container
A  .settings/org.eclipse.wst.jsdt.ui.superType.name
A  WebContent/ChatWindow.xhtml
A  WebContent/META-INF/MANIFEST.MF
A  WebContent/WEB-INF/faces-config.xml
A  WebContent/WEB-INF/sun-web.xml
A  WebContent/WEB-INF/web.xml
A  WebContent/client.html
A  WebContent/css/oneColFixCtr.css
A  WebContent/index.xhtml
A  WebContent/js/jquery-1.7.2.min.js
A  build/classes/resources/application.properties
A  build/classes/serverApp/MyWebSocketApplication.class
A  build/classes/servlet/WebSocketServlet.class
A  build/classes/socket/ClientSocket.class
A  src/resources/application.properties
A  src/serverApp/MyWebSocketApplication.java
A  src/servlet/WebSocketServlet.java
A  src/socket/ClientSocket.java

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$ git remote add origin https://github.com/mjtoolbox/WebsocketDemo.git
fatal: remote origin already exists.

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$ ls
README  WebContent  build  src

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$ git commit -m 'initially added'
[master 73776b7] initially added
warning: LF will be replaced by CRLF in WebContent/WEB-INF/faces-config.xml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in WebContent/WEB-INF/sun-web.xml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in WebContent/css/oneColFixCtr.css.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in WebContent/js/jquery-1.7.2.min.js.
The file will have its original line endings in your working directory.
 28 files changed, 598 insertions(+)
 create mode 100644 .classpath
 create mode 100644 .metadata/WebContent/WEB-INF/faces-config.pageflow
 create mode 100644 .project
 create mode 100644 .settings/.jsdtscope
 create mode 100644 .settings/oracle.eclipse.tools.webtier.ui.prefs
 create mode 100644 .settings/org.eclipse.jdt.core.prefs
 create mode 100644 .settings/org.eclipse.wst.common.component
 create mode 100644 .settings/org.eclipse.wst.common.project.facet.core.prefs.xm
l
 create mode 100644 .settings/org.eclipse.wst.common.project.facet.core.xml
 create mode 100644 .settings/org.eclipse.wst.jsdt.ui.superType.container
 create mode 100644 .settings/org.eclipse.wst.jsdt.ui.superType.name
 create mode 100644 WebContent/ChatWindow.xhtml
 create mode 100644 WebContent/META-INF/MANIFEST.MF
 create mode 100644 WebContent/WEB-INF/faces-config.xml
 create mode 100644 WebContent/WEB-INF/sun-web.xml
 create mode 100644 WebContent/WEB-INF/web.xml
 create mode 100644 WebContent/client.html
 create mode 100644 WebContent/css/oneColFixCtr.css
 create mode 100644 WebContent/index.xhtml
 create mode 100644 WebContent/js/jquery-1.7.2.min.js
 create mode 100644 build/classes/resources/application.properties
 create mode 100644 build/classes/serverApp/MyWebSocketApplication.class
 create mode 100644 build/classes/servlet/WebSocketServlet.class
 create mode 100644 build/classes/socket/ClientSocket.class
 create mode 100644 src/resources/application.properties
 create mode 100644 src/serverApp/MyWebSocketApplication.java
 create mode 100644 src/servlet/WebSocketServlet.java
 create mode 100644 src/socket/ClientSocket.java

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$ git push -u origin master
usage: git credential-cache [options]

    --exit                tell a running daemon to exit
    --reject              reject a cached credential
    --timeout          number of seconds to cache credentials
    --socket        path of cache-daemon socket
    --chain       use  to get non-cached credentials
    --username      an existing username
    --description   human-readable description of the credential
    --unique       a unique context for the credential

Username for 'https://github.com':
Password for 'https://mjtoolbox@github.com':
usage: git credential-cache [options]

    --exit                tell a running daemon to exit
    --reject              reject a cached credential
    --timeout          number of seconds to cache credentials
    --socket        path of cache-daemon socket
    --chain       use  to get non-cached credentials
    --username      an existing username
    --description   human-readable description of the credential
    --unique       a unique context for the credential

To https://github.com/mjtoolbox/WebsocketDemo.git
   824a40b..73776b7  master -> master
Branch master set up to track remote branch master from origin.

msjo@MSJO-PC ~/gitrepo/WebsocketDemo (master)
$
Advertisements

Websocket using Glassfish

This month, I wanted to try out HTML5 Websocket chat application. There are lots of Websocket tutorials out there, but I found different technology combination makes it difficult to have application run.

In my demo, I used below combinations.

* Glassfish 3.1.2

* Eclipse Indigo (OEPE 12.1.1.0.11)

* JQuery 1.7.2

My Websocket project, I started with Dynamic Web Project with Mojarra JSF implementation. In the beginning, I had a difficult time to link the concept with the code. Websocket concept is simple to understand, but coding was little bit confusing.

I was following the example from this tutorial.  After understanding the tutorial, I set up my project with following steps:

1. Add Grizzly Websocket library. Import external jars from C:\glassfish3\glassfish\modules location. I knew Glassfish included Grizzly library, but not sure how it included in the project. I downloaded new 2.2.7 version, but realized that Glassfish 3.1.2 doesn’t work with newer version of Grizzly. Glassfish 3.1.2 supports Grizzly 1.9.46.

2. Create server application, servlet, and socket classes. The example shows in the tutorial uses older version of Grizzly and some method names have been changed. For example,
public class MyWebSocketApplication extends WebSocketApplication
{
public WebSocket createSocket(ProtocolHandler handler,
WebSocketListener... listeners)
{
return new ClientSocket(handler, listeners);
}
@Override
public boolean isApplicationRequest(Request arg0)
{
return true;
}
@Override
public void onConnect(WebSocket socket)
{
super.onConnect(socket);
System.out.println("On Connect.....");
}

3. Create xhtml chat page. One of the tricky part was using JQuery with JSF form element.  For example,  JQuery selector needs to have a Form name prefix with “\\:” before actual form element ID.

$(“#chatForm\\:message”).val(“Hello there”);

<h:form id=”chatForm”>

<h:inputText type=”text” id=”message”></h:inputText>

</h:form>

This threw me off for a while and I found the solution from the internet.

4. websocket instance in the client side javascript needs to point to the Servlet URL.

ws = new WebSocket("ws://localhost:8080/WebsocketDemo/WebSocketServlet/myChat");

Of course, the servlet mapping needs to be done in web.xml. Also, enable Websocket in the Glassfish by executing below command.

asadmin set configs.config.server-config.network-config.
protocols.protocol.http-listener-1.http.websockets-support-enabled=true

My presentation at the Slideshare.

Source code at Github.