[Spread-users] java
Julien Dufour
julien at posse42.net
Thu May 10 09:17:26 EDT 2001
On jeudi, mai 10, 2001, at 07:50 , Aigars Riekstiņš wrote:
> Hello,
> I am new to java and trying to use spread java API run into
> 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
> code to be
> wrong. So I would like to ask somebody to take look at it and
> 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 = new SpreadGroup();
> SpreadMessage message = new SpreadMessage();
> SpreadConnection connection = new SpreadConnection();
>
> private void jTextArea3MouseClicked(java.awt.event.MouseEvent evt) {
> if(group != null)
> if(connection != null){
> msg_data = 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 = "";
> group_name = jTextField8.getText();
> user_name = 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
of the connection.
There are two ways to receive messages. You can poll them
manually (with the "poll" method), or you can use listeners. As
you have implemented the "BasicMessageListener" interface, you
choosed the second way. You must subscribe every listener to the
connection to make them receive the messages, joining a group in
not enough.
Joining and leaving groups only define which messages your
connection will receive. As you haven't subscribed (with the
"add" method), your "messageReceived" method isn't invoked when
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
object can be used only once. You can not connect again after
you have disconnected. So, your code will work correctly during
the first connection only.
> _______________________________________________
> spread-users mailing list
> spread-users at lists.spread.org
> http://lists.spread.org/mailman/listinfo/spread-users
>
--
Julien Dufour
Posse42
More information about the Spread-users
mailing list