Hi,

l have got a solution to solve my probleme. 

This is what l did I do not no if it is the good way but it can help 
somebody and l need your appreciation on that

1-I create a new codenameone projet using the netbeans IDE called MHTalk
2-I download the websocket lib by using the codenameone extension manager 
tool
3-I select "Refresh Libs" after finishing download the lib
4-I select the project on netbeans I "right click the mouse" and select 
"Build"
5-After the build is done I execute the project and it work verry well
6-This is the code of my MHTalk project 
package com.mhsoft.mhtalk;


import com.codename1.components.SpanLabel;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.io.websocket.WebSocket;
import com.codename1.ui.Button;
import com.codename1.ui.Command;
import com.codename1.ui.Container;
import com.codename1.ui.TextField;
import com.codename1.ui.Toolbar;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.BoxLayout;
import java.io.IOException;

/**
 * This file was generated by <a 
href="https://www.codenameone.com/";>Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class MHTalk {

    private Form current;
    private Resources theme;
    WebSocket sock;
    Container chatContainer;
    public static final String 
SERVER_URL="ws://localhost:8080/cn1-websocket-demo-server/chat";

    public void init(Object context) {
        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature, uncomment if you have a pro subscription
        // Log.bindCrashProtection(true);
        websocketconnection();
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
//        Form hi = new Form("Hi World");
//        hi.addComponent(new Label("Hi World"));
//        hi.show();
        showLogin();
    }

    public void stop() {
        current = Display.getInstance().getCurrent();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = Display.getInstance().getCurrent();
        }
    }
    
    public void destroy() {
    }
    void showLogin() {
        Form f = new Form("MHTalk");
        f.addComponent(new Label("Name: "));
        final TextField nameField = new TextField();
        f.addComponent(nameField);
        f.addComponent(new Button(new Command("Login"){ 

            @Override
            public void actionPerformed(ActionEvent evt) {
                sock.send(nameField.getText());
                showChat(nameField.getText());
            }
               
        }));
        f.show();
    }
    
    void showChat(String username) {
        Form f= new Form("Welcome "+username+" Chat Form");
        f.setLayout(new BorderLayout());
        Container south = new Container();
        final TextField tf = new TextField();
        Button send = new Button(new Command("Send") {
            @Override
            public void actionPerformed(ActionEvent evt) {
                sock.send(tf.getText());
                tf.setText("");
            }
             
        });
        
        chatContainer = new Container();
        chatContainer.setScrollableY(true);
        chatContainer.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        
        south.addComponent(tf);
        south.addComponent(send);
        f.addComponent(BorderLayout.SOUTH, south);
        f.addComponent(BorderLayout.CENTER, chatContainer);
        f.setFormBottomPaddingEditingMode(true);
        f.show();
        
    }
    
    public void websocketconnection(){
        sock = new WebSocket(SERVER_URL) {

            @Override
            protected void onOpen() {
                System.out.println("In onOpen");
            }

            @Override
            protected void onClose(int statusCode, String reason) {
                
            }

            @Override
            protected void onMessage(final String message) {
                System.out.println("Received message "+message);
                Display.getInstance().callSerially(new Runnable() {
                    public void run() {
                        if (chatContainer == null) {
                            return;
                        }
                        SpanLabel label = new SpanLabel();
                        label.setText(message);
                        chatContainer.addComponent(label);
                        chatContainer.animateHierarchy(100);
                    }                   
                });
            }

            @Override
            protected void onError(Exception ex) {
                System.out.println("in onError");
            }
             @Override
             protected void onMessage(byte[] message) {
                 
             }
            
        };
        System.out.println("Sending connect");
        sock.connect();
    }
}

Here are some screen shots of the test


<https://lh3.googleusercontent.com/-zpLRzVjJi4Y/V-t_t_-j-8I/AAAAAAAAARQ/9Tn8BWZFYxANFqdw29a_bBXv-bFVrt1YACLcB/s1600/mhtalk6.jpg>
 
<https://lh3.googleusercontent.com/-QpwsenPuxQU/V-t_TlmgrbI/AAAAAAAAARM/_hsRHG3ZtrQ9JLMeEPsZklso3sIzbUi9QCLcB/s1600/mhtalk5.jpg>
 
<https://lh3.googleusercontent.com/-E7QSEpfIJOQ/V-t-_CVH8LI/AAAAAAAAARI/TWDuNN1tchUpkts_Qa-o17aGOm7MqQkFACLcB/s1600/mhtalk4.jpg>

-- 
You received this message because you are subscribed to the Google Groups 
"CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/codenameone-discussions/4309a680-5ac1-4831-9d7a-113ede7c6398%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to