[Spread-users] java
Julien Dufour
julien@posse42.net
Thu, 10 May 2001 15:17:26 +0200
On jeudi, mai 10, 2001, at 07:50 , Aigars Rieksti=C5=86=C5=A1 wrote:
> Hello,
> I am new to java and trying to use spread java API run into=20
> problem. I can
> join, send, disconnect from spread group and other members see it but =
I
> cannot receive messages. As total beginner in Java I suspect my=20
> code to be
> wrong. So I would like to ask somebody to take look at it and=20
> give some help
> or point to resources where to find answer.
> Thank You
> Aigars Riekstinsh
> My code----------------------------------------
> package spread.gui1;
> import spread.*;
> import java.net.*;
> import java.io.*;
> import java.util.*;
> public class JFrame extends javax.swing.JFrame implements
> BasicMessageListener{
> public JFrame() {
> initComponents ();
> pack ();
> }
>
> SpreadGroup group =3D new SpreadGroup();
> SpreadMessage message =3D new SpreadMessage();
> SpreadConnection connection =3D new SpreadConnection();
>
> private void jTextArea3MouseClicked(java.awt.event.MouseEvent =
evt) {
> if(group !=3D null)
> if(connection !=3D null){
> msg_data =3D jTextArea3.getText();
> message.setSafe();
> message.addGroup(group);
> message.setData(new String(msg_data).getBytes());
> connection.multicast(message);
> ....................}
> private void jButton1MouseClicked(java.awt.event.MouseEvent evt) =
{
> group.leave();
> connection.disconnect();
> ....................}
> private void jButton5MouseClicked(java.awt.event.MouseEvent evt) =
{
> group_name =3D "";
> group_name =3D jTextField8.getText();
> user_name =3D jTextField4.getText();
> connection.connect(InetAddress.getByName("localhost"), =
0, user_name,
> false, true);
> group.join(connection, group_name);
> ...................}
> public static void main (String args[]) {
> new JFrame ().show ();}
> private void DisplayMessage(SpreadMessage message)
> {................}
> public void messageReceived(SpreadMessage message) {
> DisplayMessage(message);
> }
> private java.lang.String group_name;
> private java.lang.String user_name;
> private java.lang.String msg_data;
> }
>
You must add the instance of your class in the list of listeners=20
of the connection.
There are two ways to receive messages. You can poll them=20
manually (with the "poll" method), or you can use listeners. As=20
you have implemented the "BasicMessageListener" interface, you=20
choosed the second way. You must subscribe every listener to the=20
connection to make them receive the messages, joining a group in=20
not enough.
Joining and leaving groups only define which messages your=20
connection will receive. As you haven't subscribed (with the=20
"add" method), your "messageReceived" method isn't invoked when=20
a message is received.
Just add the following line before your "connection.connect" call:
connection.add(this);
PS: Be careful. As you can read in the javadoc, the Connection=20
object can be used only once. You can not connect again after=20
you have disconnected. So, your code will work correctly during=20
the first connection only.
> _______________________________________________
> spread-users mailing list
> spread-users@lists.spread.org
> http://lists.spread.org/mailman/listinfo/spread-users
>
--
Julien Dufour
Posse42